chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动

演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-18 13:59:25 +08:00
1 parent 60158935f4
commit 6519682885
15831 files changed
+3063428

No files matched your search

Whitespace-only changes.
@@ -0,0 +1,81 @@
from typing import Literal
from flask_restx import Resource
from pydantic import BaseModel, Field
from controllers.common.schema import (
DEFAULT_REF_TEMPLATE_OPENAPI_3_0,
query_params_from_model,
register_response_schema_models,
)
from controllers.console import console_ns
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import validate_request
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from machinery.context import RequestContext
class AdvancedPromptTemplateQuery(BaseModel):
app_mode: str = Field(..., description="Application mode")
model_mode: str = Field(..., description="Model mode")
has_context: str = Field(default="true", description="Whether has context")
model_name: str = Field(..., description="Model name")
class PromptTemplateMessageResponse(ResponseModel):
role: Literal["system", "user", "assistant"]
text: str
class ChatPromptTemplateResponse(ResponseModel):
prompt: list[PromptTemplateMessageResponse]
class CompletionPromptTemplateTextResponse(ResponseModel):
text: str
class ConversationHistoriesRoleResponse(ResponseModel):
user_prefix: str
assistant_prefix: str
class CompletionPromptTemplateResponse(ResponseModel):
prompt: CompletionPromptTemplateTextResponse
conversation_histories_role: ConversationHistoriesRoleResponse | None = None
class AdvancedPromptTemplateResponse(ResponseModel):
chat_prompt_config: ChatPromptTemplateResponse | None = Field(default=None)
completion_prompt_config: CompletionPromptTemplateResponse | None = Field(default=None)
stop: list[str] | None = Field(default=None, description="Stop words for completion prompts.")
console_ns.schema_model(
AdvancedPromptTemplateQuery.__name__,
AdvancedPromptTemplateQuery.model_json_schema(ref_template=DEFAULT_REF_TEMPLATE_OPENAPI_3_0),
)
register_response_schema_models(console_ns, AdvancedPromptTemplateResponse)
@console_ns.route("/app/prompt-templates")
class AdvancedPromptTemplateList(Resource):
@console_ns.doc("get_advanced_prompt_templates")
@console_ns.doc(description="Get advanced prompt templates based on app mode and model configuration")
@console_ns.doc(params=query_params_from_model(AdvancedPromptTemplateQuery))
@console_ns.response(
200,
"Prompt templates retrieved successfully",
console_ns.models[AdvancedPromptTemplateResponse.__name__],
)
@console_ns.response(400, "Invalid request parameters")
@console_account_admission()
def get(self, context: RequestContext):
req_data = validate_request(AdvancedPromptTemplateQuery)
result = application_services().advanced_prompt_templates.get_prompt(
app_mode=req_data.app_mode,
model_mode=req_data.model_mode,
has_context=req_data.has_context,
)
return AdvancedPromptTemplateResponse.model_validate(result).model_dump(mode="json", exclude_unset=True)
@@ -0,0 +1,94 @@
from typing import Any
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from sqlalchemy.orm import Session
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_response_schema_models
from controllers.common.session import with_session
from controllers.console import console_ns
from controllers.console.app.wraps import get_app_model
from controllers.console.wraps import (
RBACPermission,
account_initialization_required,
model_validate,
rbac_permission_required,
setup_required,
)
from fields.base import ResponseModel
from libs.helper import uuid_value
from libs.login import login_required
from models.model import App, AppMode
from services.agent_service import AgentService
class AgentLogQuery(BaseModel):
message_id: str = Field(..., description="Message UUID")
conversation_id: str = Field(..., description="Conversation UUID")
@field_validator("message_id", "conversation_id")
@classmethod
def validate_uuid(cls, value: str) -> str:
return uuid_value(value)
class AgentLogMetaResponse(ResponseModel):
status: str
executor: str
start_time: str
elapsed_time: float | None = None
total_tokens: int
agent_mode: str
iterations: int
class AgentToolCallResponse(ResponseModel):
status: str
error: str | None = None
time_cost: float | int
tool_name: str
tool_label: str | dict[str, str] = Field(description="Tool name or its i18n labels keyed by locale")
tool_input: dict[str, Any]
tool_output: dict[str, Any]
tool_parameters: dict[str, Any]
tool_icon: str | dict[str, str] | None = Field(default=None, description="Icon URL or emoji icon")
class AgentIterationLogResponse(ResponseModel):
tokens: int
tool_calls: list[AgentToolCallResponse]
tool_raw: dict[str, Any]
thought: str | None = None
created_at: str
files: list[Any] = Field(default_factory=list)
class AgentLogResponse(ResponseModel):
meta: AgentLogMetaResponse
iterations: list[AgentIterationLogResponse]
files: list[Any] = Field(default_factory=list)
register_response_schema_models(console_ns, AgentLogResponse)
@console_ns.route("/apps/<uuid:app_id>/agent/logs")
class AgentLogApi(Resource):
@console_ns.doc("get_agent_logs")
@console_ns.doc(description="Get agent execution logs for an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(AgentLogQuery))
@console_ns.response(200, "Agent logs retrieved successfully", console_ns.models[AgentLogResponse.__name__])
@console_ns.response(400, "Invalid request parameters")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@with_session(write=False)
@get_app_model(mode=[AppMode.AGENT_CHAT])
@model_validate(AgentLogQuery)
def get(self, req_data: AgentLogQuery, session: Session, app_model: App):
"""Get agent logs."""
return AgentService.get_agent_logs(app_model, req_data.conversation_id, req_data.message_id, session)
@@ -0,0 +1,65 @@
"""Agent App access & sharing endpoints (read-only workflow references).
An Agent App is backed by a roster Agent that workflow Agent nodes may also
reference. This exposes the read-only "Workflow access" surface from the PRD:
which workflow apps use this Agent, without leaking the workflows' internals.
"""
from uuid import UUID
from flask_restx import Resource
from pydantic import Field
from controllers.common.rbac import AgentId, RBACCheck
from controllers.common.schema import register_response_schema_models
from controllers.console import console_ns
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import RBACPermission
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from libs.external_api import error_responses
from libs.helper import dump_response
from machinery.context import RequestContext
from services.agent.errors import AgentNotFoundError
from services.app.agent_app_contracts import AgentAppNotFoundError
class AgentReferencingWorkflowResponse(ResponseModel):
app_id: str
app_name: str
app_icon_type: str | None = None
app_icon: str | None = None
app_icon_background: str | None = None
app_mode: str
app_updated_at: int | None = None
workflow_id: str
workflow_version: str
node_ids: list[str] = Field(default_factory=list)
class AgentReferencingWorkflowsResponse(ResponseModel):
data: list[AgentReferencingWorkflowResponse] = Field(default_factory=list)
register_response_schema_models(console_ns, AgentReferencingWorkflowsResponse)
@console_ns.route("/agent/<uuid:agent_id>/referencing-workflows")
class AgentAppReferencingWorkflowsResource(Resource):
@error_responses(AgentNotFoundError)
@console_ns.doc("list_agent_app_referencing_workflows")
@console_ns.doc(description="List workflow apps that reference this Agent App's bound Agent (read-only)")
@console_ns.doc(params={"agent_id": "Agent ID"})
@console_ns.response(
200,
"Referencing workflows listed successfully",
console_ns.models[AgentReferencingWorkflowsResponse.__name__],
)
@console_ns.response(404, "Agent not found")
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.AGENT_PREVIEW, AgentId()),))
def get(self, context: RequestContext, agent_id: UUID):
try:
workflows = application_services().agent_apps.access.list_referencing_workflows(context, str(agent_id))
except AgentAppNotFoundError as exc:
raise AgentNotFoundError from exc
return dump_response(AgentReferencingWorkflowsResponse, {"data": workflows})
@@ -0,0 +1,87 @@
"""Agent App presentation-feature configuration endpoint.
The new Agent App type keeps model / prompt / tools in its bound Agent Soul, so
the legacy ``/model-config`` surface (which writes model, prompt and agent tool
config) is the wrong place to configure its app-level presentation features.
This endpoint exposes only the PRD "Misc Legacy" feature subset — conversation
opener, follow-up suggestions, citations, content moderation and speech — and
persists them onto the app's ``app_model_config`` without touching anything the
Soul owns.
"""
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field
from controllers.common.fields import SimpleResultResponse
from controllers.common.rbac import AgentId, RBACCheck
from controllers.common.schema import register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import RBACPermission, validate_request
from extensions.ext_application_services import application_services
from libs.external_api import error_responses
from machinery.context import RequestContext
from models.account import TenantAccountRole
from models.agent_config_entities import (
AgentFeatureToggleConfig,
AgentSensitiveWordAvoidanceFeatureConfig,
AgentSuggestedQuestionsAfterAnswerFeatureConfig,
AgentTextToSpeechFeatureConfig,
)
from services.agent.errors import AgentNotFoundError
from services.app.agent_app_contracts import AgentAppNotFoundError
class AgentAppFeaturesPayload(BaseModel):
"""Presentation features configurable on an Agent App.
All fields are optional; an omitted field is reset to its disabled/empty
default (the config form sends the full desired feature state on save).
"""
opening_statement: str | None = Field(default=None, description="Conversation opener shown before the first turn")
suggested_questions: list[str] | None = Field(
default=None, description="Preset questions shown alongside the opener"
)
suggested_questions_after_answer: AgentSuggestedQuestionsAfterAnswerFeatureConfig | None = Field(
default=None, description="Follow-up suggestions config, e.g. {'enabled': true}"
)
speech_to_text: AgentFeatureToggleConfig | None = Field(default=None, description="Speech-to-text config")
text_to_speech: AgentTextToSpeechFeatureConfig | None = Field(default=None, description="Text-to-speech config")
retriever_resource: AgentFeatureToggleConfig | None = Field(
default=None, description="Citations / attributions config, e.g. {'enabled': true}"
)
sensitive_word_avoidance: AgentSensitiveWordAvoidanceFeatureConfig | None = Field(
default=None, description="Content moderation config"
)
register_schema_models(console_ns, AgentAppFeaturesPayload)
register_response_schema_models(console_ns, SimpleResultResponse)
@console_ns.route("/agent/<uuid:agent_id>/features")
class AgentAppFeatureConfigResource(Resource):
@error_responses(AgentNotFoundError)
@console_ns.doc("update_agent_app_features")
@console_ns.doc(description="Update an Agent App's presentation features (opener, follow-up, citations, ...)")
@console_ns.doc(params={"agent_id": "Agent ID"})
@console_ns.expect(console_ns.models[AgentAppFeaturesPayload.__name__])
@console_ns.response(200, "Features updated successfully", console_ns.models[SimpleResultResponse.__name__])
@console_ns.response(400, "Invalid configuration")
@console_ns.response(404, "Agent not found")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.AGENT_EDIT, AgentId()),),
)
def post(self, context: RequestContext, agent_id: UUID):
payload = validate_request(AgentAppFeaturesPayload)
try:
application_services().agent_apps.features.update_features(
context, str(agent_id), payload.model_dump(exclude_none=True)
)
except AgentAppNotFoundError as exc:
raise AgentNotFoundError from exc
return SimpleResultResponse(result="success").model_dump(mode="json")
@@ -0,0 +1,351 @@
"""Console routes for Agent App and workflow Agent sandbox file access.
The API accepts product-facing Conversation, Build Draft, or Workflow Node
Execution locators and proxies list/read/download to the agent backend's
``/execution-bindings/files`` contract.
"""
from __future__ import annotations
from typing import Literal, NoReturn
from uuid import UUID
from dify_agent.client import DifyAgentClientError, DifyAgentHTTPError
from flask_restx import Resource
from pydantic import BaseModel, Field
from clients.agent_backend.errors import backend_error_detail, backend_reported_failure
from controllers.common.rbac import AgentId, PlainApp, RBACCheck
from controllers.common.schema import (
query_params_from_model,
query_params_from_request,
register_response_schema_models,
register_schema_models,
)
from controllers.console import console_ns
from controllers.console.app.error import (
AgentBackendUnreachableError,
AgentSandboxBindingNotFoundHTTPError,
AgentSandboxDownloadUnavailableHTTPError,
AgentSandboxUnavailableHTTPError,
AppNotFoundError,
)
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import RBACPermission, validate_request
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from libs.exception import RelayedHTTPError
from libs.external_api import error_responses
from libs.helper import dump_response
from machinery.context import RequestContext
from services.agent.errors import AgentNotFoundError
from services.app.agent_app_contracts import (
AgentAppNotFoundError,
AgentSandboxBindingNotFoundError,
AgentSandboxCaller,
AgentSandboxDownloadUnavailableError,
AgentSandboxUnavailableError,
WorkflowSandboxAppNotFoundError,
WorkflowSandboxCaller,
)
_BINDING_PATH_DESCRIPTION = (
"Binding path: relative paths start in Workspace; exact `~` and paths beginning with `~/` start in Home; "
"`~user` is an ordinary relative path from Workspace; absolute paths remain absolute; `..` and paths outside "
"Workspace are governed by backend isolation, not a Workspace-root restriction"
)
class AgentSandboxListQuery(BaseModel):
caller_type: Literal["conversation", "build_draft"]
caller_id: str = Field(min_length=1, description="Agent App caller ID")
path: str = Field(default=".", description=_BINDING_PATH_DESCRIPTION)
class AgentSandboxInfoQuery(BaseModel):
caller_type: Literal["conversation", "build_draft"]
caller_id: str = Field(min_length=1, description="Agent App caller ID")
class AgentSandboxFileQuery(BaseModel):
caller_type: Literal["conversation", "build_draft"]
caller_id: str = Field(min_length=1, description="Agent App caller ID")
path: str = Field(min_length=1, description=_BINDING_PATH_DESCRIPTION)
class AgentSandboxDownloadPayload(BaseModel):
caller_type: Literal["conversation", "build_draft"]
caller_id: str = Field(min_length=1, description="Agent App caller ID")
path: str = Field(min_length=1, description=_BINDING_PATH_DESCRIPTION)
class WorkflowAgentSandboxListQuery(BaseModel):
node_execution_id: str = Field(min_length=1, description="Workflow node execution ID")
path: str = Field(default=".", description=_BINDING_PATH_DESCRIPTION)
class WorkflowAgentSandboxFileQuery(BaseModel):
node_execution_id: str = Field(min_length=1, description="Workflow node execution ID")
path: str = Field(min_length=1, description=_BINDING_PATH_DESCRIPTION)
class WorkflowAgentSandboxDownloadPayload(BaseModel):
node_execution_id: str = Field(min_length=1, description="Workflow node execution ID")
path: str = Field(min_length=1, description=_BINDING_PATH_DESCRIPTION)
class SandboxFileEntryResponse(ResponseModel):
name: str
type: Literal["file", "dir", "symlink", "other"]
size: int | None = None
mtime: int | None = None
class SandboxListResponse(ResponseModel):
path: str
entries: list[SandboxFileEntryResponse] = Field(default_factory=list)
truncated: bool = False
class SandboxInfoResponse(ResponseModel):
workspace_cwd: str
class SandboxReadResponse(ResponseModel):
path: str
size: int | None = None
truncated: bool
binary: bool
text: str | None = None
class SandboxDownloadResponse(ResponseModel):
url: str
register_schema_models(
console_ns,
AgentSandboxDownloadPayload,
WorkflowAgentSandboxDownloadPayload,
)
register_response_schema_models(
console_ns,
SandboxInfoResponse,
SandboxListResponse,
SandboxReadResponse,
SandboxDownloadResponse,
)
def _handle(exc: Exception) -> NoReturn:
if isinstance(exc, AgentAppNotFoundError):
raise AgentNotFoundError from exc
if isinstance(exc, WorkflowSandboxAppNotFoundError):
raise AppNotFoundError from exc
if isinstance(exc, AgentSandboxBindingNotFoundError):
raise AgentSandboxBindingNotFoundHTTPError(str(exc)) from exc
if isinstance(exc, AgentSandboxUnavailableError):
raise AgentSandboxUnavailableHTTPError(str(exc)) from exc
if isinstance(exc, AgentSandboxDownloadUnavailableError):
raise AgentSandboxDownloadUnavailableHTTPError(str(exc)) from exc
if isinstance(exc, DifyAgentHTTPError) and backend_reported_failure(exc):
code, message = backend_error_detail(exc)
raise RelayedHTTPError(status=exc.status_code, error_code=code, message=message) from exc
if isinstance(exc, DifyAgentClientError):
raise AgentBackendUnreachableError(str(exc)) from exc
raise exc
@console_ns.route("/agent/<uuid:agent_id>/sandbox")
class AgentAppSandboxInfoResource(Resource):
@error_responses(AgentNotFoundError, AgentSandboxBindingNotFoundHTTPError)
@console_ns.doc("get_agent_app_sandbox_info")
@console_ns.doc(description="Get basic information for an Agent App conversation sandbox")
@console_ns.doc(params={"agent_id": "Agent ID", **query_params_from_model(AgentSandboxInfoQuery)})
@console_ns.response(200, "Sandbox information returned", console_ns.models[SandboxInfoResponse.__name__])
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.AGENT_PREVIEW, AgentId()),))
def get(self, context: RequestContext, agent_id: UUID):
query = query_params_from_request(AgentSandboxInfoQuery)
try:
result = application_services().agent_apps.sandbox.get_info(
context, AgentSandboxCaller(str(agent_id), query.caller_type, query.caller_id)
)
except Exception as exc:
_handle(exc)
return dump_response(SandboxInfoResponse, result)
@console_ns.route("/agent/<uuid:agent_id>/sandbox/files")
class AgentAppSandboxListResource(Resource):
@error_responses(
AgentBackendUnreachableError,
AgentNotFoundError,
AgentSandboxBindingNotFoundHTTPError,
AgentSandboxUnavailableHTTPError,
RelayedHTTPError,
)
@console_ns.doc("list_agent_app_sandbox_files")
@console_ns.doc(description="List a directory in an Agent App conversation sandbox")
@console_ns.doc(params={"agent_id": "Agent ID", **query_params_from_model(AgentSandboxListQuery)})
@console_ns.response(200, "Listing returned", console_ns.models[SandboxListResponse.__name__])
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.AGENT_PREVIEW, AgentId()),))
def get(self, context: RequestContext, agent_id: UUID):
query = query_params_from_request(AgentSandboxListQuery)
try:
result = application_services().agent_apps.sandbox.list_files(
context, AgentSandboxCaller(str(agent_id), query.caller_type, query.caller_id), query.path
)
except Exception as exc:
_handle(exc)
return dump_response(SandboxListResponse, result)
@console_ns.route("/agent/<uuid:agent_id>/sandbox/files/read")
class AgentAppSandboxReadResource(Resource):
@error_responses(
AgentBackendUnreachableError,
AgentNotFoundError,
AgentSandboxBindingNotFoundHTTPError,
AgentSandboxUnavailableHTTPError,
RelayedHTTPError,
)
@console_ns.doc("read_agent_app_sandbox_file")
@console_ns.doc(description="Read a text/binary preview file in an Agent App conversation sandbox")
@console_ns.doc(params={"agent_id": "Agent ID", **query_params_from_model(AgentSandboxFileQuery)})
@console_ns.response(200, "Preview returned", console_ns.models[SandboxReadResponse.__name__])
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.AGENT_PREVIEW, AgentId()),))
def get(self, context: RequestContext, agent_id: UUID):
query = query_params_from_request(AgentSandboxFileQuery)
try:
result = application_services().agent_apps.sandbox.read_file(
context, AgentSandboxCaller(str(agent_id), query.caller_type, query.caller_id), query.path
)
except Exception as exc:
_handle(exc)
return dump_response(SandboxReadResponse, result)
@console_ns.route("/agent/<uuid:agent_id>/sandbox/files/download")
class AgentAppSandboxDownloadResource(Resource):
@error_responses(
AgentBackendUnreachableError,
AgentNotFoundError,
AgentSandboxBindingNotFoundHTTPError,
AgentSandboxDownloadUnavailableHTTPError,
AgentSandboxUnavailableHTTPError,
RelayedHTTPError,
)
@console_ns.doc("download_agent_app_sandbox_file")
@console_ns.doc(description="Create a ToolFile from one Agent App Binding file and return its download URL")
@console_ns.expect(console_ns.models[AgentSandboxDownloadPayload.__name__])
@console_ns.response(200, "Download URL returned", console_ns.models[SandboxDownloadResponse.__name__])
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.AGENT_EDIT, AgentId()),))
def post(self, context: RequestContext, agent_id: UUID):
query = validate_request(AgentSandboxDownloadPayload)
try:
result = application_services().agent_apps.sandbox.download_file(
context, AgentSandboxCaller(str(agent_id), query.caller_type, query.caller_id), query.path
)
except Exception as exc:
_handle(exc)
return dump_response(SandboxDownloadResponse, result)
@console_ns.route("/apps/<uuid:app_id>/workflow-runs/<uuid:workflow_run_id>/agent-nodes/<string:node_id>/sandbox/files")
class WorkflowAgentSandboxListResource(Resource):
@error_responses(
AgentBackendUnreachableError,
AgentSandboxBindingNotFoundHTTPError,
AgentSandboxUnavailableHTTPError,
AppNotFoundError,
RelayedHTTPError,
)
@console_ns.doc("list_workflow_agent_sandbox_files")
@console_ns.doc(description="List a directory in a workflow Agent node sandbox")
@console_ns.doc(
params={
"app_id": "Application ID",
"workflow_run_id": "Workflow run ID",
"node_id": "Workflow Agent node ID",
**query_params_from_model(WorkflowAgentSandboxListQuery),
}
)
@console_ns.response(200, "Listing returned", console_ns.models[SandboxListResponse.__name__])
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),))
def get(self, context: RequestContext, app_id: UUID, workflow_run_id: UUID, node_id: str):
query = query_params_from_request(WorkflowAgentSandboxListQuery)
try:
result = application_services().agent_apps.sandbox.list_files(
context,
WorkflowSandboxCaller(str(app_id), str(workflow_run_id), node_id, query.node_execution_id),
query.path,
)
except Exception as exc:
_handle(exc)
return dump_response(SandboxListResponse, result)
@console_ns.route(
"/apps/<uuid:app_id>/workflow-runs/<uuid:workflow_run_id>/agent-nodes/<string:node_id>/sandbox/files/read"
)
class WorkflowAgentSandboxReadResource(Resource):
@error_responses(
AgentBackendUnreachableError,
AgentSandboxBindingNotFoundHTTPError,
AgentSandboxUnavailableHTTPError,
AppNotFoundError,
RelayedHTTPError,
)
@console_ns.doc("read_workflow_agent_sandbox_file")
@console_ns.doc(description="Read a text/binary preview file in a workflow Agent node sandbox")
@console_ns.doc(
params={
"app_id": "Application ID",
"workflow_run_id": "Workflow run ID",
"node_id": "Workflow Agent node ID",
**query_params_from_model(WorkflowAgentSandboxFileQuery),
}
)
@console_ns.response(200, "Preview returned", console_ns.models[SandboxReadResponse.__name__])
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),))
def get(self, context: RequestContext, app_id: UUID, workflow_run_id: UUID, node_id: str):
query = query_params_from_request(WorkflowAgentSandboxFileQuery)
try:
result = application_services().agent_apps.sandbox.read_file(
context,
WorkflowSandboxCaller(str(app_id), str(workflow_run_id), node_id, query.node_execution_id),
query.path,
)
except Exception as exc:
_handle(exc)
return dump_response(SandboxReadResponse, result)
@console_ns.route(
"/apps/<uuid:app_id>/workflow-runs/<uuid:workflow_run_id>/agent-nodes/<string:node_id>/sandbox/files/download"
)
class WorkflowAgentSandboxDownloadResource(Resource):
@error_responses(
AgentBackendUnreachableError,
AgentSandboxBindingNotFoundHTTPError,
AgentSandboxDownloadUnavailableHTTPError,
AgentSandboxUnavailableHTTPError,
AppNotFoundError,
RelayedHTTPError,
)
@console_ns.doc("download_workflow_agent_sandbox_file")
@console_ns.doc(description="Create a ToolFile from one workflow Agent Binding file and return its download URL")
@console_ns.expect(console_ns.models[WorkflowAgentSandboxDownloadPayload.__name__])
@console_ns.response(200, "Download URL returned", console_ns.models[SandboxDownloadResponse.__name__])
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),))
def post(self, context: RequestContext, app_id: UUID, workflow_run_id: UUID, node_id: str):
query = validate_request(WorkflowAgentSandboxDownloadPayload)
try:
result = application_services().agent_apps.sandbox.download_file(
context,
WorkflowSandboxCaller(str(app_id), str(workflow_run_id), node_id, query.node_execution_id),
query.path,
)
except Exception as exc:
_handle(exc)
return dump_response(SandboxDownloadResponse, result)
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,628 @@
from http import HTTPStatus
from typing import Any, Literal
from uuid import UUID
from flask import request
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from controllers.common.errors import (
FileTooLargeError,
InternalServerError,
NoFileUploadedError,
NotFoundError,
TooManyFilesError,
)
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AnnotationIdsRequiredError, AnnotationImportRateLimitError, AppNotFoundError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import (
RBACPermission,
model_validate,
)
from core.helper.csv_sanitizer import CSVSanitizer
from enums.account import TenantAccountRole
from extensions.ext_application_services import application_services
from fields.annotation_fields import (
Annotation,
AnnotationExportList,
AnnotationHitHistory,
AnnotationHitHistoryList,
AnnotationJobStatusDetailResponse,
AnnotationJobStatusResponse,
AnnotationList,
)
from fields.base import ResponseModel
from libs.external_api import error_responses
from libs.helper import dump_response, uuid_value
from machinery.context import RequestContext
from services.annotation_command_service import AnnotationSettingNotFoundError
from services.annotation_import_service import (
AnnotationImportJobNotFoundError,
AnnotationImportLimitError,
AnnotationImportValidationError,
)
from services.annotation_query import AnnotationAppNotFoundError, AnnotationNotFoundError
from services.annotation_reply_service import AnnotationReplyJobNotFoundError
from services.errors.message import MessageNotExistsError
class AnnotationReplyPayload(BaseModel):
score_threshold: float = Field(..., description="Score threshold for annotation matching")
embedding_provider_name: str = Field(..., description="Embedding provider name")
embedding_model_name: str = Field(..., description="Embedding model name")
class AnnotationSettingUpdatePayload(BaseModel):
score_threshold: float = Field(..., description="Score threshold")
class AnnotationListQuery(BaseModel):
page: int = Field(default=1, ge=1, description="Page number")
limit: int = Field(default=20, ge=1, description="Page size")
keyword: str = Field(default="", description="Search keyword")
class AnnotationBatchDeleteQuery(BaseModel):
annotation_id: list[str] = Field(
default_factory=list,
description="Annotation IDs to delete; omitting it clears all annotations of the app",
)
class CreateAnnotationPayload(BaseModel):
message_id: str | None = Field(default=None, description="Message ID")
question: str | None = Field(default=None, description="Question text")
answer: str | None = Field(default=None, description="Answer text")
content: str | None = Field(default=None, description="Content text")
annotation_reply: dict[str, Any] | None = Field(
default=None,
description="Annotation reply data",
)
@field_validator("message_id")
@classmethod
def validate_message_id(cls, value: str | None) -> str | None:
if value is None:
return value
return uuid_value(value)
class UpdateAnnotationPayload(BaseModel):
question: str | None = None
answer: str | None = None
content: str | None = None
annotation_reply: dict[str, Any] | None = Field(default=None)
class AnnotationReplyStatusQuery(BaseModel):
action: Literal["enable", "disable"]
class AnnotationHitHistoryListQuery(BaseModel):
page: int = Field(default=1, ge=1, description="Page number")
limit: int = Field(default=20, ge=1, description="Page size")
class AnnotationFilePayload(BaseModel):
message_id: str = Field(..., description="Message ID")
@field_validator("message_id")
@classmethod
def validate_message_id(cls, value: str) -> str:
return uuid_value(value)
class AnnotationSettingEmbeddingModelResponse(ResponseModel):
embedding_provider_name: str | None = None
embedding_model_name: str | None = None
class AnnotationSettingResponse(ResponseModel):
enabled: bool
id: str | None = None
score_threshold: float | None = None
embedding_model: AnnotationSettingEmbeddingModelResponse | None = None
class AnnotationCountResponse(ResponseModel):
count: int = Field(description="Number of annotations")
class AnnotationBatchImportResponse(ResponseModel):
job_id: str | None = None
job_status: str | None = None
record_count: int | None = None
error_msg: str | None = None
register_schema_models(
console_ns,
AnnotationReplyPayload,
AnnotationSettingUpdatePayload,
AnnotationListQuery,
CreateAnnotationPayload,
UpdateAnnotationPayload,
AnnotationReplyStatusQuery,
AnnotationHitHistoryListQuery,
AnnotationFilePayload,
)
register_response_schema_models(
console_ns,
Annotation,
AnnotationList,
AnnotationExportList,
AnnotationHitHistory,
AnnotationHitHistoryList,
AnnotationJobStatusResponse,
AnnotationJobStatusDetailResponse,
AnnotationSettingEmbeddingModelResponse,
AnnotationSettingResponse,
AnnotationBatchImportResponse,
AnnotationCountResponse,
)
@console_ns.route("/apps/<uuid:app_id>/annotations/count")
class MessageAnnotationCountApi(Resource):
@error_responses(AppNotFoundError)
@console_ns.doc("get_annotation_count")
@console_ns.doc(description="Get count of message annotations for the app")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
HTTPStatus.OK,
"Annotation count retrieved successfully",
console_ns.models[AnnotationCountResponse.__name__],
)
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),))
def get(self, context: RequestContext, app_id: UUID) -> dict[str, object]:
try:
count = application_services().annotation_queries.count(
tenant_id=context.active_workspace_id, app_id=str(app_id)
)
except AnnotationAppNotFoundError as exc:
raise AppNotFoundError() from exc
return dump_response(AnnotationCountResponse, {"count": count})
@console_ns.route("/apps/<uuid:app_id>/annotation-reply/<string:action>")
class AnnotationReplyActionApi(Resource):
@console_ns.doc("annotation_reply_action")
@console_ns.doc(description="Enable or disable annotation reply for an app")
@console_ns.doc(params={"app_id": "Application ID", "action": "Action to perform (enable/disable)"})
@console_ns.expect(console_ns.models[AnnotationReplyPayload.__name__])
@console_ns.response(
HTTPStatus.OK, "Action completed successfully", console_ns.models[AnnotationJobStatusResponse.__name__]
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App not found")
@console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid action")
@console_ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, "Invalid payload")
@console_account_admission(
billing_resource="annotation",
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_EDIT, PlainApp()),),
)
@model_validate(AnnotationReplyPayload)
def post(
self, req_data: AnnotationReplyPayload, context: RequestContext, app_id: UUID, action: str
) -> tuple[dict[str, object], HTTPStatus]:
validated_action = AnnotationReplyStatusQuery.model_validate({"action": action}).action
service = application_services().annotation_reply
try:
if validated_action == "enable":
result = service.enable(
tenant_id=context.active_workspace_id,
app_id=str(app_id),
account_id=context.account_id,
score_threshold=req_data.score_threshold,
embedding_provider_name=req_data.embedding_provider_name,
embedding_model_name=req_data.embedding_model_name,
)
else:
result = service.disable(tenant_id=context.active_workspace_id, app_id=str(app_id))
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
return dump_response(AnnotationJobStatusResponse, result), HTTPStatus.OK
@console_ns.route("/apps/<uuid:app_id>/annotation-setting")
class AppAnnotationSettingDetailApi(Resource):
@console_ns.doc("get_annotation_setting")
@console_ns.doc(description="Get annotation settings for an app")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
HTTPStatus.OK,
"Annotation settings retrieved successfully",
console_ns.models[AnnotationSettingResponse.__name__],
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
def get(self, context: RequestContext, app_id: UUID) -> tuple[dict[str, object], HTTPStatus]:
try:
result = application_services().annotation_queries.get_setting(
tenant_id=context.active_workspace_id, app_id=str(app_id)
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
return dump_response(AnnotationSettingResponse, result), HTTPStatus.OK
@console_ns.route("/apps/<uuid:app_id>/annotation-settings/<uuid:annotation_setting_id>")
class AppAnnotationSettingUpdateApi(Resource):
@console_ns.doc("update_annotation_setting")
@console_ns.doc(description="Update annotation settings for an app")
@console_ns.doc(params={"app_id": "Application ID", "annotation_setting_id": "Annotation setting ID"})
@console_ns.expect(console_ns.models[AnnotationSettingUpdatePayload.__name__])
@console_ns.response(
HTTPStatus.OK, "Settings updated successfully", console_ns.models[AnnotationSettingResponse.__name__]
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_EDIT, PlainApp()),),
)
@model_validate(AnnotationSettingUpdatePayload)
def post(
self,
req_data: AnnotationSettingUpdatePayload,
context: RequestContext,
app_id: UUID,
annotation_setting_id: UUID,
) -> tuple[dict[str, object], HTTPStatus]:
try:
result = application_services().annotation_commands.update_setting(
tenant_id=context.active_workspace_id,
app_id=str(app_id),
setting_id=str(annotation_setting_id),
account_id=context.account_id,
score_threshold=req_data.score_threshold,
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
except AnnotationSettingNotFoundError as exc:
raise NotFoundError("App annotation not found") from exc
return dump_response(AnnotationSettingResponse, result), HTTPStatus.OK
@console_ns.route("/apps/<uuid:app_id>/annotation-reply/<string:action>/status/<uuid:job_id>")
class AnnotationReplyActionStatusApi(Resource):
@console_ns.doc("get_annotation_reply_action_status")
@console_ns.doc(description="Get status of annotation reply action job")
@console_ns.doc(params={"app_id": "Application ID", "job_id": "Job ID", "action": "Action type"})
@console_ns.response(
HTTPStatus.OK,
"Job status retrieved successfully",
console_ns.models[AnnotationJobStatusDetailResponse.__name__],
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App or owned job not found")
@console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid action")
@console_account_admission(
billing_resource="annotation",
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
def get(
self, context: RequestContext, app_id: UUID, job_id: UUID, action: str
) -> tuple[dict[str, object], HTTPStatus]:
validated_action = AnnotationReplyStatusQuery.model_validate({"action": action}).action
try:
result = application_services().annotation_reply.get_status(
tenant_id=context.active_workspace_id,
app_id=str(app_id),
action=validated_action,
job_id=str(job_id),
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
except AnnotationReplyJobNotFoundError as exc:
raise NotFoundError("The job does not exist.") from exc
return dump_response(AnnotationJobStatusDetailResponse, result), HTTPStatus.OK
@console_ns.route("/apps/<uuid:app_id>/annotations")
class AnnotationApi(Resource):
@console_ns.doc("list_annotations")
@console_ns.doc(description="Get annotations for an app with pagination")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(AnnotationListQuery))
@console_ns.response(
HTTPStatus.OK, "Annotations retrieved successfully", console_ns.models[AnnotationList.__name__]
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
@model_validate(AnnotationListQuery)
def get(
self, req_data: AnnotationListQuery, context: RequestContext, app_id: UUID
) -> tuple[dict[str, object], HTTPStatus]:
try:
result = application_services().annotation_queries.get_page(
tenant_id=context.active_workspace_id,
app_id=str(app_id),
page=req_data.page,
limit=req_data.limit,
keyword=req_data.keyword,
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
return dump_response(AnnotationList, result), HTTPStatus.OK
@console_ns.doc("create_annotation")
@console_ns.doc(description="Create a new annotation for an app")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[CreateAnnotationPayload.__name__])
@console_ns.response(HTTPStatus.CREATED, "Annotation created successfully", console_ns.models[Annotation.__name__])
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
billing_resource="annotation",
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_EDIT, PlainApp()),),
)
@model_validate(CreateAnnotationPayload)
def post(
self, req_data: CreateAnnotationPayload, context: RequestContext, app_id: UUID
) -> tuple[dict[str, object], HTTPStatus]:
try:
annotation = application_services().annotation_commands.upsert(
tenant_id=context.active_workspace_id,
app_id=str(app_id),
account_id=context.account_id,
message_id=req_data.message_id,
question=req_data.question,
answer=req_data.answer,
content=req_data.content,
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
except MessageNotExistsError as exc:
raise NotFoundError("Message Not Exists.") from exc
return dump_response(Annotation, annotation), HTTPStatus.CREATED
@error_responses(AnnotationIdsRequiredError)
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp()),),
)
@console_ns.doc(params=query_params_from_model(AnnotationBatchDeleteQuery))
@console_ns.response(HTTPStatus.NO_CONTENT, "Annotations deleted successfully")
def delete(self, context: RequestContext, app_id: UUID) -> tuple[str, HTTPStatus]:
annotation_ids = request.args.getlist("annotation_id")
if not all(annotation_id.strip() for annotation_id in annotation_ids if annotation_id):
raise AnnotationIdsRequiredError()
try:
commands = application_services().annotation_commands
if annotation_ids:
commands.delete_many(
tenant_id=context.active_workspace_id, app_id=str(app_id), annotation_ids=annotation_ids
)
else:
commands.clear(tenant_id=context.active_workspace_id, app_id=str(app_id))
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
return "", HTTPStatus.NO_CONTENT
@console_ns.route("/apps/<uuid:app_id>/annotations/export")
class AnnotationExportApi(Resource):
@console_ns.doc("export_annotations")
@console_ns.doc(description="Export all annotations for an app with CSV injection protection")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
HTTPStatus.OK,
"Annotations exported successfully",
console_ns.models[AnnotationExportList.__name__],
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
def get(self, context: RequestContext, app_id: UUID) -> tuple[dict[str, object], HTTPStatus, dict[str, str]]:
try:
annotations = application_services().annotation_queries.get_all(
tenant_id=context.active_workspace_id, app_id=str(app_id)
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
result = AnnotationExportList.model_validate({"data": annotations})
# The client turns this JSON into CSV; escape only the detached response.
for annotation in result.data:
if annotation.question:
annotation.question = CSVSanitizer.sanitize_value(annotation.question)
if annotation.answer:
annotation.answer = CSVSanitizer.sanitize_value(annotation.answer)
return (
dump_response(AnnotationExportList, result),
HTTPStatus.OK,
{
"Content-Type": "application/json; charset=utf-8",
"X-Content-Type-Options": "nosniff",
},
)
@console_ns.route("/apps/<uuid:app_id>/annotations/<uuid:annotation_id>")
class AnnotationUpdateDeleteApi(Resource):
@console_ns.doc("update_delete_annotation")
@console_ns.doc(description="Update or delete an annotation")
@console_ns.doc(params={"app_id": "Application ID", "annotation_id": "Annotation ID"})
@console_ns.response(HTTPStatus.OK, "Annotation updated successfully", console_ns.models[Annotation.__name__])
@console_ns.response(HTTPStatus.NO_CONTENT, "Annotation deleted successfully")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.expect(console_ns.models[UpdateAnnotationPayload.__name__])
@console_account_admission(
billing_resource="annotation",
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_EDIT, PlainApp()),),
)
@model_validate(UpdateAnnotationPayload)
def post(
self, req_data: UpdateAnnotationPayload, context: RequestContext, app_id: UUID, annotation_id: UUID
) -> dict[str, object]:
try:
annotation = application_services().annotation_commands.update(
tenant_id=context.active_workspace_id,
app_id=str(app_id),
annotation_id=str(annotation_id),
question=req_data.question,
answer=req_data.answer,
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
except AnnotationNotFoundError as exc:
raise NotFoundError("Annotation not found") from exc
return dump_response(Annotation, annotation)
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_EDIT, PlainApp()),),
)
@console_ns.response(HTTPStatus.NO_CONTENT, "Annotation deleted successfully")
def delete(self, context: RequestContext, app_id: UUID, annotation_id: UUID) -> tuple[str, HTTPStatus]:
try:
application_services().annotation_commands.delete(
tenant_id=context.active_workspace_id, app_id=str(app_id), annotation_id=str(annotation_id)
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
except AnnotationNotFoundError as exc:
raise NotFoundError("Annotation not found") from exc
return "", HTTPStatus.NO_CONTENT
@console_ns.route("/apps/<uuid:app_id>/annotations/batch-import")
class AnnotationBatchImportApi(Resource):
@error_responses(NoFileUploadedError, TooManyFilesError)
@console_ns.doc("batch_import_annotations")
@console_ns.doc(description="Batch import annotations from CSV file with rate limiting and security checks")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
HTTPStatus.OK,
"Import started, or CSV/subscription validation failed (error_msg)",
console_ns.models[AnnotationBatchImportResponse.__name__],
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App not found")
@console_ns.response(HTTPStatus.BAD_REQUEST, "Missing, multiple, empty or invalid file")
@console_ns.response(HTTPStatus.REQUEST_ENTITY_TOO_LARGE, "File too large")
@console_ns.response(HTTPStatus.TOO_MANY_REQUESTS, "Too many requests or concurrent imports")
@console_ns.response(HTTPStatus.INTERNAL_SERVER_ERROR, "Import infrastructure failed")
@console_account_admission(
billing_resource="annotation",
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_EDIT, PlainApp()),),
)
def post(self, context: RequestContext, app_id: UUID) -> tuple[dict[str, object], HTTPStatus]:
from configs import dify_config
if "file" not in request.files:
raise NoFileUploadedError()
if len(list(request.files.items(multi=True))) > 1:
raise TooManyFilesError()
file = request.files["file"]
if not file.filename or not file.filename.lower().endswith(".csv"):
raise ValueError("Invalid file type. Only CSV files are allowed")
file.stream.seek(0, 2)
file_size = file.stream.tell()
file.stream.seek(0)
if file_size > dify_config.ANNOTATION_IMPORT_FILE_SIZE_LIMIT * 1024 * 1024:
raise FileTooLargeError(
f"File size exceeds maximum limit of {dify_config.ANNOTATION_IMPORT_FILE_SIZE_LIMIT}MB. "
"Please reduce the file size and try again."
)
if file_size == 0:
raise ValueError("The uploaded file is empty")
try:
result = application_services().annotation_imports.import_csv(
tenant_id=context.active_workspace_id,
app_id=str(app_id),
account_id=context.account_id,
stream=file.stream,
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
except AnnotationImportLimitError as exc:
raise AnnotationImportRateLimitError(str(exc)) from exc
except AnnotationImportValidationError as exc:
# Retain the existing import response for CSV and record-quota validation failures.
return dump_response(AnnotationBatchImportResponse, {"error_msg": str(exc)}), HTTPStatus.OK
except Exception as exc:
# Infrastructure ValueErrors must not become client-side invalid_param responses.
raise InternalServerError() from exc
return dump_response(AnnotationBatchImportResponse, result), HTTPStatus.OK
@console_ns.route("/apps/<uuid:app_id>/annotations/batch-import-status/<uuid:job_id>")
class AnnotationBatchImportStatusApi(Resource):
@console_ns.doc("get_batch_import_status")
@console_ns.doc(description="Get status of batch import job")
@console_ns.doc(params={"app_id": "Application ID", "job_id": "Job ID"})
@console_ns.response(
HTTPStatus.OK,
"Job status retrieved successfully",
console_ns.models[AnnotationJobStatusDetailResponse.__name__],
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App or owned job not found")
@console_ns.response(HTTPStatus.INTERNAL_SERVER_ERROR, "Import status could not be read")
@console_account_admission(
billing_resource="annotation",
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
def get(self, context: RequestContext, app_id: UUID, job_id: UUID) -> tuple[dict[str, object], HTTPStatus]:
try:
result = application_services().annotation_imports.get_status(
tenant_id=context.active_workspace_id, app_id=str(app_id), job_id=str(job_id)
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
except AnnotationImportJobNotFoundError as exc:
raise NotFoundError("The job does not exist.") from exc
except Exception as exc:
raise InternalServerError() from exc
return dump_response(AnnotationJobStatusDetailResponse, result), HTTPStatus.OK
@console_ns.route("/apps/<uuid:app_id>/annotations/<uuid:annotation_id>/hit-histories")
class AnnotationHitHistoryListApi(Resource):
@console_ns.doc("list_annotation_hit_histories")
@console_ns.doc(description="Get hit histories for an annotation")
@console_ns.doc(params={"app_id": "Application ID", "annotation_id": "Annotation ID"})
@console_ns.doc(params=query_params_from_model(AnnotationHitHistoryListQuery))
@console_ns.response(
HTTPStatus.OK,
"Hit histories retrieved successfully",
console_ns.models[AnnotationHitHistoryList.__name__],
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
def get(self, context: RequestContext, app_id: UUID, annotation_id: UUID) -> dict[str, object]:
# Preserve this endpoint's forgiving parsing; the repository clamps bounds.
page = request.args.get("page", default=1, type=int)
limit = request.args.get("limit", default=20, type=int)
try:
result = application_services().annotation_queries.get_hit_history_page(
tenant_id=context.active_workspace_id,
app_id=str(app_id),
annotation_id=str(annotation_id),
page=page,
limit=limit,
)
except AnnotationAppNotFoundError as exc:
raise NotFoundError("App not found") from exc
except AnnotationNotFoundError as exc:
raise NotFoundError("Annotation not found") from exc
return dump_response(AnnotationHitHistoryList, result)
@@ -0,0 +1,718 @@
import uuid
from dataclasses import asdict
from http import HTTPStatus
from typing import Literal, Self, override
from flask import request, send_file
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator, model_validator
from werkzeug.exceptions import BadRequest, Forbidden, NotFound
from configs import dify_config
from controllers.common.fields import RedirectUrlResponse, SimpleResultResponse
from controllers.common.rbac import AgentBehindApp, PlainApp, RBACCheck, Workspace
from controllers.common.schema import (
query_params_from_model,
query_params_from_request,
register_enum_models,
register_response_schema_models,
register_schema_models,
)
from controllers.console import console_ns
from controllers.console.app.error import (
AppNotFoundError,
InvalidTracingConfigError,
TracingConfigConflictError,
TracingConfigNotFoundError,
TracingConfigProcessingError,
TracingProviderUnavailableError,
UnsupportedTracingProviderError,
)
from controllers.console.flask_admission import console_account_admission
from controllers.console.workspace.models import LoadBalancingPayload
from controllers.console.wraps import (
RBACPermission,
validate_request,
)
from core.file.remote_file_metadata import FileInfo
from core.rag.entities import PreProcessingRule, Rule, Segmentation
from core.rag.retrieval.retrieval_methods import RetrievalMethod
from extensions.ext_application_services import application_services
from fields.app_fields import (
AppDetail,
AppDetailWithSite,
AppExportResponse,
AppImportResponse,
AppModelConfigResponse,
AppPagination,
AppPartial,
AppTraceResponse,
DeletedTool,
RecentAppListResponse,
RecentAppResponse,
Tag,
WorkflowPartial,
)
from graphon.enums import WorkflowExecutionStatus
from libs.external_api import error_responses
from libs.flask_restx_compat import BINARY_RESPONSE_MEDIA_TYPES_VENDOR_KEY
from libs.helper import dump_response
from libs.url_utils import normalize_api_base_url
from machinery.context import RequestContext
from models import DatasetPermissionEnum
from models.account import TenantAccountRole
from models.model import IconType
from services.agent.errors import RosterAgentPackageExportFailedError
from services.app.console_service import (
AppExportAgentNotFoundError,
AppExportPaidPlanRequiredError,
ConsoleAppNotFoundError,
CreatorsPlatformDisabledError,
InvalidAppAccessModesError,
InvalidAppExportError,
)
from services.app_tracing_config_service import (
AppTraceSettings,
AppTracingConfigAppNotFoundError,
AppTracingConfigConflictError,
AppTracingConfigInvalidConfigurationError,
AppTracingConfigInvalidProviderError,
AppTracingConfigNotFoundError,
AppTracingConfigProcessingError,
AppTracingConfigProviderUnavailableError,
)
from services.entities.app_entities import (
AppExportOptions,
AppListParams,
AppListSortBy,
AppRecord,
CopyAppParams,
CreateAppParams,
StarredAppListParams,
UpdateAppParams,
)
from services.entities.dsl_entities import ImportStatus
from services.errors.base import NoPermissionError
from services.knowledge.entities.knowledge_entities import (
DataSource,
InfoList,
NotionIcon,
NotionInfo,
NotionPage,
RerankingModel,
WebsiteInfo,
WeightKeywordSetting,
WeightModel,
WeightVectorSetting,
)
register_enum_models(console_ns, IconType)
AppListMode = Literal["completion", "chat", "advanced-chat", "workflow", "agent-chat", "agent", "channel", "all"]
DEFAULT_APP_LIST_MODE: AppListMode = "all"
APP_LIST_QUERY_ARRAY_FIELDS = ("tag_ids", "creator_ids")
class AppListBaseQuery(BaseModel):
page: int = Field(default=1, ge=1, le=99999, description="Page number (1-99999)")
limit: int = Field(default=20, ge=1, le=100, description="Page size (1-100)")
mode: AppListMode = Field(default=DEFAULT_APP_LIST_MODE, description="App mode filter")
sort_by: AppListSortBy = Field(
default="last_modified",
description="Sort apps by last modified, recently created, or earliest created",
)
name: str | None = Field(default=None, description="Filter by app name")
openapi_visible: bool = Field(default=False, description="Only return apps with API access enabled")
tag_ids: list[str] | None = Field(default=None, description="Filter by tag IDs")
creator_ids: list[str] | None = Field(default=None, description="Filter by creator account IDs")
is_created_by_me: bool | None = Field(default=None, description="Filter by creator")
@field_validator("tag_ids", mode="before")
@classmethod
def validate_tag_ids(cls, value: list[str] | None) -> list[str] | None:
if not value:
return None
if not isinstance(value, list):
raise ValueError("Unsupported tag_ids type.")
items = [str(item).strip() for item in value if item and str(item).strip()]
if not items:
return None
try:
return [str(uuid.UUID(item)) for item in items]
except ValueError as exc:
raise ValueError("Invalid UUID format in tag_ids.") from exc
@field_validator("creator_ids", mode="before")
@classmethod
def validate_creator_ids(cls, value: list[str] | None) -> list[str] | None:
if not value:
return None
if not isinstance(value, list):
raise ValueError("Unsupported creator_ids type.")
items = [str(item).strip() for item in value if item and str(item).strip()]
if not items:
return None
try:
return [str(uuid.UUID(item)) for item in items]
except ValueError as exc:
raise ValueError("Invalid UUID format in creator_ids.") from exc
class RecentAppListQuery(BaseModel):
limit: int = Field(default=8, ge=1, le=8, description="Number of recently modified apps to return (1-8)")
class AppListQuery(AppListBaseQuery):
pass
class StarredAppListQuery(AppListBaseQuery):
pass
class CreateAppPayload(BaseModel):
name: str = Field(..., min_length=1, description="App name")
description: str | None = Field(default=None, description="App description (max 400 chars)", max_length=400)
mode: Literal["chat", "agent-chat", "advanced-chat", "workflow", "completion"] = Field(..., description="App mode")
icon_type: IconType | None = Field(default=None, description="Icon type")
icon: str | None = Field(default=None, description="Icon")
icon_background: str | None = Field(default=None, description="Icon background color")
class UpdateAppPayload(BaseModel):
name: str = Field(..., min_length=1, description="App name")
description: str | None = Field(default=None, description="App description (max 400 chars)", max_length=400)
icon_type: IconType | None = Field(default=None, description="Icon type")
icon: str | None = Field(default=None, description="Icon")
icon_background: str | None = Field(default=None, description="Icon background color")
use_icon_as_answer_icon: bool | None = Field(default=None, description="Use icon as answer icon")
max_active_requests: int | None = Field(default=None, description="Maximum active requests")
class CopyAppPayload(BaseModel):
name: str | None = Field(default=None, description="Name for the copied app")
description: str | None = Field(default=None, description="Description for the copied app", max_length=400)
icon_type: IconType | None = Field(default=None, description="Icon type")
icon: str | None = Field(default=None, description="Icon")
icon_background: str | None = Field(default=None, description="Icon background color")
class AppExportQuery(BaseModel):
format: Literal["yaml", "ifpkg"] | None = Field(
default=None, description="Export format; defaults to ifpkg for all Apps"
)
include_secret: bool = Field(default=False, description="Include secrets in export")
workflow_id: str | None = Field(default=None, description="Specific workflow ID to export")
version_id: uuid.UUID | None = Field(
default=None,
description=(
"Published Agent version ID to export; requires a paid plan on Cloud. "
"If omitted, exports the shared draft, falling back to the active snapshot when no draft exists."
),
)
@model_validator(mode="after")
def validate_version_selectors(self) -> Self:
if self.version_id is not None and self.workflow_id is not None:
raise ValueError("version_id and workflow_id cannot be used together")
return self
class AppNamePayload(BaseModel):
name: str = Field(..., min_length=1, description="Name to check")
class AppIconPayload(BaseModel):
icon: str | None = Field(default=None, description="Icon data")
icon_type: IconType | None = Field(default=None, description="Icon type")
icon_background: str | None = Field(default=None, description="Icon background color")
class AppSiteStatusPayload(BaseModel):
enable_site: bool = Field(..., description="Enable or disable site")
class AppApiStatusPayload(BaseModel):
enable_api: bool = Field(..., description="Enable or disable API")
class AppTracePayload(BaseModel):
enabled: bool = Field(..., description="Enable or disable tracing")
tracing_provider: str | None = Field(default=None, description="Tracing provider")
capture_content: bool = Field(default=False, description="Include prompt, output and tool content in new traces")
capture_agent: bool = Field(default=True, description="Capture Agent internals when deployment support is enabled")
@model_validator(mode="after")
def validate_tracing_provider(self) -> "AppTracePayload":
if self.enabled and not self.tracing_provider:
raise ValueError("tracing_provider is required when enabled is True")
return self
register_enum_models(console_ns, RetrievalMethod, WorkflowExecutionStatus, DatasetPermissionEnum)
register_response_schema_models(
console_ns,
RedirectUrlResponse,
SimpleResultResponse,
AppImportResponse,
AppTraceResponse,
AppModelConfigResponse,
AppDetail,
)
register_schema_models(
console_ns,
AppListQuery,
StarredAppListQuery,
CreateAppPayload,
UpdateAppPayload,
CopyAppPayload,
AppExportQuery,
AppNamePayload,
AppIconPayload,
AppSiteStatusPayload,
AppApiStatusPayload,
AppTracePayload,
Tag,
WorkflowPartial,
DeletedTool,
AppExportResponse,
Segmentation,
PreProcessingRule,
Rule,
WeightVectorSetting,
WeightKeywordSetting,
WeightModel,
RerankingModel,
InfoList,
NotionInfo,
FileInfo,
WebsiteInfo,
NotionPage,
NotionIcon,
RerankingModel,
DataSource,
LoadBalancingPayload,
)
register_response_schema_models(
console_ns,
AppPartial,
RecentAppResponse,
RecentAppListResponse,
AppDetailWithSite,
AppPagination,
)
_EDIT_ROLES = frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR})
_ADMIN_ROLES = frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN})
def _app_detail_response(app: AppRecord) -> AppDetailWithSite:
"""Resolve transport URLs here so repositories work without a Flask request."""
data = asdict(app)
data["api_base_url"] = normalize_api_base_url(dify_config.SERVICE_API_URL or request.host_url.rstrip("/"))
data["app_id"] = None
if data["site"] is not None:
data["site"]["app_base_url"] = dify_config.APP_WEB_URL or request.url_root.rstrip("/")
return AppDetailWithSite.model_validate(data)
class AppResource(Resource):
"""Translate application failures at the HTTP boundary for all app routes."""
@override
def dispatch_request(self, *args, **kwargs):
try:
return super().dispatch_request(*args, **kwargs)
except AppExportAgentNotFoundError as error:
raise NotFound(str(error)) from error
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
except (InvalidAppExportError, InvalidAppAccessModesError) as error:
raise BadRequest(str(error)) from error
except (AppExportPaidPlanRequiredError, NoPermissionError) as error:
raise Forbidden(str(error)) from error
except CreatorsPlatformDisabledError as error:
return {"error": str(error)}, HTTPStatus.FORBIDDEN
except AppTracingConfigInvalidProviderError as error:
raise ValueError(str(error)) from error
@console_ns.route("/apps")
class AppListApi(AppResource):
@console_ns.doc("list_apps")
@console_ns.doc(description="Get list of applications with pagination and filtering")
@console_ns.doc(params=query_params_from_model(AppListQuery))
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[AppPagination.__name__])
@console_account_admission(require_valid_enterprise_license=True)
def get(self, context: RequestContext):
args = query_params_from_request(AppListQuery, list_fields=APP_LIST_QUERY_ARRAY_FIELDS)
params = AppListParams.model_validate(args.model_dump())
return dump_response(
AppPagination, application_services().apps.console.list_apps(context, params)
), HTTPStatus.OK
@console_ns.doc("create_app")
@console_ns.doc(description="Create a new application")
@console_ns.expect(console_ns.models[CreateAppPayload.__name__])
@console_ns.response(HTTPStatus.CREATED, "App created successfully", console_ns.models[AppDetailWithSite.__name__])
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid request parameters")
@console_account_admission(
allowed_roles=_EDIT_ROLES,
billing_resource="apps",
rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, Workspace())],
)
def post(self, context: RequestContext):
payload = validate_request(CreateAppPayload)
params = CreateAppParams.model_validate(payload.model_dump())
response = _app_detail_response(application_services().apps.console.create(context, params))
return dump_response(AppDetailWithSite, response), HTTPStatus.CREATED
@console_ns.route("/apps/recent")
class RecentAppListApi(AppResource):
@console_ns.doc("list_recent_apps")
@console_ns.doc(description="Get recently modified apps for the home Continue Work section")
@console_ns.doc(params=query_params_from_model(RecentAppListQuery))
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[RecentAppListResponse.__name__])
@console_account_admission(require_valid_enterprise_license=True)
def get(self, context: RequestContext):
args = query_params_from_request(RecentAppListQuery)
return dump_response(
RecentAppListResponse, {"data": application_services().apps.console.recent(context, args.limit)}
), HTTPStatus.OK
@console_ns.route("/apps/starred")
class StarredAppListApi(AppResource):
@console_ns.doc("list_starred_apps")
@console_ns.doc(description="Get applications starred by the current account")
@console_ns.doc(params=query_params_from_model(StarredAppListQuery))
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[AppPagination.__name__])
@console_account_admission(require_valid_enterprise_license=True)
def get(self, context: RequestContext):
args = query_params_from_request(StarredAppListQuery, list_fields=APP_LIST_QUERY_ARRAY_FIELDS)
params = StarredAppListParams.model_validate(args.model_dump())
return dump_response(
AppPagination, application_services().apps.console.list_apps(context, params)
), HTTPStatus.OK
@console_ns.route("/apps/<uuid:app_id>/star")
class AppStarApi(AppResource):
@console_ns.doc("star_app")
@console_ns.doc(description="Star an application for the current account")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[SimpleResultResponse.__name__])
@console_ns.response(HTTPStatus.NOT_FOUND, "App not found")
@console_account_admission(require_valid_enterprise_license=True)
def post(self, context: RequestContext, app_id: uuid.UUID):
application_services().apps.console.set_starred(context, str(app_id), True)
return SimpleResultResponse(result="success").model_dump(mode="json")
@console_ns.doc("unstar_app")
@console_ns.doc(description="Remove the current account's star from an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[SimpleResultResponse.__name__])
@console_ns.response(HTTPStatus.NOT_FOUND, "App not found")
@console_account_admission(require_valid_enterprise_license=True)
def delete(self, context: RequestContext, app_id: uuid.UUID):
application_services().apps.console.set_starred(context, str(app_id), False)
return SimpleResultResponse(result="success").model_dump(mode="json")
@console_ns.route("/apps/<uuid:app_id>")
class AppApi(AppResource):
@console_ns.doc("get_app_detail")
@console_ns.doc(description="Get application details")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[AppDetailWithSite.__name__])
@console_account_admission(require_valid_enterprise_license=True)
def get(self, context: RequestContext, app_id: uuid.UUID):
response = _app_detail_response(application_services().apps.console.get(context, str(app_id)))
return dump_response(AppDetailWithSite, response)
@console_ns.doc("update_app")
@console_ns.doc(description="Update application details")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[UpdateAppPayload.__name__])
@console_ns.response(HTTPStatus.OK, "App updated successfully", console_ns.models[AppDetailWithSite.__name__])
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid request parameters")
@console_account_admission(
allowed_roles=_EDIT_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_EDIT, PlainApp()),
RBACCheck(RBACPermission.AGENT_EDIT, AgentBehindApp()),
],
)
def put(self, context: RequestContext, app_id: uuid.UUID):
payload = validate_request(UpdateAppPayload)
params = UpdateAppParams(
name=payload.name,
description=payload.description or "",
icon_type=payload.icon_type,
icon=payload.icon or "",
icon_background=payload.icon_background or "",
use_icon_as_answer_icon=payload.use_icon_as_answer_icon or False,
max_active_requests=payload.max_active_requests or 0,
)
response = _app_detail_response(application_services().apps.console.update(context, str(app_id), params))
return dump_response(AppDetailWithSite, response)
@console_ns.doc("delete_app")
@console_ns.doc(description="Delete application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(HTTPStatus.NO_CONTENT, "App deleted successfully")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=_EDIT_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_DELETE, PlainApp()),
RBACCheck(RBACPermission.AGENT_DELETE, AgentBehindApp()),
],
)
def delete(self, context: RequestContext, app_id: uuid.UUID):
application_services().apps.console.delete(context, str(app_id))
return "", HTTPStatus.NO_CONTENT
@console_ns.route("/apps/<uuid:app_id>/copy")
class AppCopyApi(AppResource):
@console_ns.doc("copy_app")
@console_ns.doc(description="Create a copy of an existing application")
@console_ns.doc(params={"app_id": "Application ID to copy"})
@console_ns.expect(console_ns.models[CopyAppPayload.__name__])
@console_ns.response(HTTPStatus.CREATED, "App copied successfully", console_ns.models[AppDetailWithSite.__name__])
@console_ns.response(
HTTPStatus.ACCEPTED, "App copy requires confirmation", console_ns.models[AppImportResponse.__name__]
)
@console_ns.response(HTTPStatus.BAD_REQUEST, "App copy failed", console_ns.models[AppImportResponse.__name__])
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=_EDIT_ROLES, rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())]
)
def post(self, context: RequestContext, app_id: uuid.UUID):
payload = validate_request(CopyAppPayload)
result, copied = application_services().apps.console.copy(
context, str(app_id), CopyAppParams.model_validate(payload.model_dump())
)
if result.status == ImportStatus.FAILED:
return dump_response(AppImportResponse, result), HTTPStatus.BAD_REQUEST
if result.status == ImportStatus.PENDING:
return dump_response(AppImportResponse, result), HTTPStatus.ACCEPTED
assert copied is not None
return dump_response(AppDetailWithSite, _app_detail_response(copied)), HTTPStatus.CREATED
@console_ns.route("/apps/<uuid:app_id>/export")
class AppExportApi(AppResource):
@error_responses(RosterAgentPackageExportFailedError)
@console_ns.doc("export_app")
@console_ns.doc(description="Export application configuration as DSL")
@console_ns.doc(params={"app_id": "Application ID to export"})
@console_ns.doc(params=query_params_from_model(AppExportQuery))
@console_ns.doc(
produces=["application/json", "application/zip"],
vendor={BINARY_RESPONSE_MEDIA_TYPES_VENDOR_KEY: ["application/zip"]},
)
@console_ns.response(HTTPStatus.OK, "App exported successfully", console_ns.models[AppExportResponse.__name__])
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=_EDIT_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_IMPORT_EXPORT_DSL, PlainApp()),
RBACCheck(RBACPermission.AGENT_IMPORT_EXPORT_DSL, AgentBehindApp()),
],
)
def get(self, context: RequestContext, app_id: uuid.UUID):
query = validate_request(AppExportQuery)
exported = application_services().apps.console.export(
context, str(app_id), AppExportOptions(**query.model_dump())
)
if isinstance(exported, str):
return AppExportResponse(data=exported).model_dump(mode="json")
try:
response = send_file(
exported.archive, mimetype="application/zip", as_attachment=True, download_name=exported.filename
)
except Exception:
exported.close()
raise
response.call_on_close(exported.close)
return response
@console_ns.route("/apps/<uuid:app_id>/publish-to-creators-platform")
class AppPublishToCreatorsPlatformApi(AppResource):
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[RedirectUrlResponse.__name__])
@console_account_admission(
allowed_roles=_EDIT_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_IMPORT_EXPORT_DSL, PlainApp()),
RBACCheck(RBACPermission.AGENT_RELEASE_AND_VERSION, AgentBehindApp()),
],
)
def post(self, context: RequestContext, app_id: uuid.UUID):
url = application_services().apps.console.publish(context, str(app_id))
return RedirectUrlResponse(redirect_url=url).model_dump(mode="json")
@console_ns.route("/apps/<uuid:app_id>/name")
class AppNameApi(AppResource):
@console_ns.doc("check_app_name")
@console_ns.doc(description="Check if app name is available")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[AppNamePayload.__name__])
@console_ns.response(HTTPStatus.OK, "Name availability checked", console_ns.models[AppDetail.__name__])
@console_account_admission(
allowed_roles=_EDIT_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_EDIT, PlainApp()),
RBACCheck(RBACPermission.AGENT_EDIT, AgentBehindApp()),
],
)
def post(self, context: RequestContext, app_id: uuid.UUID):
payload = validate_request(AppNamePayload)
return dump_response(AppDetail, application_services().apps.console.rename(context, str(app_id), payload.name))
@console_ns.route("/apps/<uuid:app_id>/icon")
class AppIconApi(AppResource):
@console_ns.doc("update_app_icon")
@console_ns.doc(description="Update application icon")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[AppIconPayload.__name__])
@console_ns.response(HTTPStatus.OK, "Icon updated successfully", console_ns.models[AppDetail.__name__])
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=_EDIT_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_EDIT, PlainApp()),
RBACCheck(RBACPermission.AGENT_EDIT, AgentBehindApp()),
],
)
def post(self, context: RequestContext, app_id: uuid.UUID):
payload = validate_request(AppIconPayload)
result = application_services().apps.console.update_icon(
context,
str(app_id),
icon=payload.icon or "",
icon_background=payload.icon_background or "",
icon_type=payload.icon_type,
)
return dump_response(AppDetail, result)
@console_ns.route("/apps/<uuid:app_id>/site-enable")
class AppSiteStatus(AppResource):
@console_ns.doc("update_app_site_status")
@console_ns.doc(description="Enable or disable app site")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[AppSiteStatusPayload.__name__])
@console_ns.response(HTTPStatus.OK, "Site status updated successfully", console_ns.models[AppDetail.__name__])
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=_EDIT_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_RELEASE_AND_VERSION, PlainApp()),
RBACCheck(RBACPermission.AGENT_ACCESS_POINT_MANAGE, AgentBehindApp()),
],
)
def post(self, context: RequestContext, app_id: uuid.UUID):
payload = validate_request(AppSiteStatusPayload)
return dump_response(
AppDetail, application_services().apps.console.set_site_enabled(context, str(app_id), payload.enable_site)
)
@console_ns.route("/apps/<uuid:app_id>/api-enable")
class AppApiStatus(AppResource):
@console_ns.doc("update_app_api_status")
@console_ns.doc(description="Enable or disable app API")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[AppApiStatusPayload.__name__])
@console_ns.response(HTTPStatus.OK, "API status updated successfully", console_ns.models[AppDetail.__name__])
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=_ADMIN_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_RELEASE_AND_VERSION, PlainApp()),
RBACCheck(RBACPermission.AGENT_ACCESS_POINT_MANAGE, AgentBehindApp()),
],
)
def post(self, context: RequestContext, app_id: uuid.UUID):
payload = validate_request(AppApiStatusPayload)
return dump_response(
AppDetail, application_services().apps.console.set_api_enabled(context, str(app_id), payload.enable_api)
)
@console_ns.route("/apps/<uuid:app_id>/trace")
class AppTraceApi(AppResource):
@console_ns.doc("get_app_trace")
@console_ns.doc(description="Get app tracing configuration")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
HTTPStatus.OK,
"Trace configuration retrieved successfully",
console_ns.models[AppTraceResponse.__name__],
)
@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp())])
def get(self, context: RequestContext, app_id: uuid.UUID):
return dump_response(AppTraceResponse, application_services().apps.console.get_trace(context, str(app_id)))
@error_responses(
AppNotFoundError,
TracingProviderUnavailableError,
TracingConfigNotFoundError,
TracingConfigConflictError,
TracingConfigProcessingError,
InvalidTracingConfigError,
UnsupportedTracingProviderError,
)
@console_ns.doc("update_app_trace")
@console_ns.doc(description="Update app tracing configuration")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[AppTracePayload.__name__])
@console_ns.response(
HTTPStatus.OK,
"Trace configuration updated successfully",
console_ns.models[SimpleResultResponse.__name__],
)
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission(
allowed_roles=_EDIT_ROLES, rbac_checks=[RBACCheck(RBACPermission.APP_TRACING_CONFIG, PlainApp())]
)
def post(self, context: RequestContext, app_id: uuid.UUID):
payload = validate_request(AppTracePayload)
try:
application_services().apps.console.set_trace(
context,
str(app_id),
AppTraceSettings(**payload.model_dump()),
)
except AppTracingConfigAppNotFoundError as error:
raise AppNotFoundError() from error
except AppTracingConfigConflictError as error:
raise TracingConfigConflictError() from error
except AppTracingConfigNotFoundError as error:
raise TracingConfigNotFoundError() from error
except AppTracingConfigProcessingError as error:
raise TracingConfigProcessingError() from error
except AppTracingConfigInvalidConfigurationError as error:
raise InvalidTracingConfigError() from error
except AppTracingConfigInvalidProviderError as error:
raise UnsupportedTracingProviderError() from error
except AppTracingConfigProviderUnavailableError as error:
raise TracingProviderUnavailableError() from error
return SimpleResultResponse(result="success").model_dump(mode="json")
@@ -0,0 +1,152 @@
from http import HTTPStatus
from typing import BinaryIO, cast
from flask import request
from flask_restx import Resource
from werkzeug.exceptions import Forbidden
from controllers.common.rbac import PlainApp, RBACCheck, Workspace
from controllers.common.schema import register_enum_models, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AppNotFoundError, DraftWorkflowNotSync
from controllers.console.explore.error import RecommendedAppNotFoundError as RecommendedAppNotFoundHttpError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import RBACPermission, validate_request
from extensions.ext_application_services import application_services
from libs.external_api import error_responses
from machinery.context import RequestContext
from models.account import TenantAccountRole
from services.agent.errors import (
AgentNameConflictError,
InvalidRosterAgentPackageError,
RosterAgentPackageImportFailedError,
RosterAgentPackageResourceUnavailableError,
RosterAgentPackageTooLargeError,
)
from services.app.console_service import ConsoleAppNotFoundError
from services.entities.dsl_entities import AppImportParams, CheckDependenciesResult, Import, ImportStatus
from services.errors.app import DraftChangedError
from services.errors.base import NoPermissionError
from services.recommended_app_query_service import RecommendedAppNotFoundError
class AppImportPayload(AppImportParams):
pass
register_enum_models(console_ns, ImportStatus)
register_schema_models(console_ns, AppImportPayload, CheckDependenciesResult)
register_response_schema_models(console_ns, Import)
_EDIT_ROLES = frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR})
@console_ns.route("/apps/imports")
class AppImportApi(Resource):
@error_responses(
AgentNameConflictError,
InvalidRosterAgentPackageError,
RecommendedAppNotFoundHttpError,
RosterAgentPackageImportFailedError,
RosterAgentPackageResourceUnavailableError,
RosterAgentPackageTooLargeError,
)
@console_ns.doc(
params={
"payload": {
"required": True,
"content": {
"application/json": {"schema": {"$ref": "#/definitions/AppImportPayload"}},
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary",
"description": "App .ifpkg archive",
},
"app_id": {"type": "string", "description": "App to overwrite"},
"name": {"type": "string"},
"description": {"type": "string"},
"icon_type": {"type": "string"},
"icon": {"type": "string"},
"icon_background": {"type": "string"},
},
"required": ["file"],
}
},
},
}
},
)
@console_ns.response(HTTPStatus.OK, "Import completed", console_ns.models[Import.__name__])
@console_ns.response(HTTPStatus.ACCEPTED, "Import pending confirmation", console_ns.models[Import.__name__])
@console_ns.response(HTTPStatus.BAD_REQUEST, "Import failed", console_ns.models[Import.__name__])
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient import permissions")
@console_ns.response(HTTPStatus.CONFLICT, "Agent name conflict")
@console_ns.response(HTTPStatus.REQUEST_ENTITY_TOO_LARGE, "Roster Agent package exceeds the size limit")
@console_account_admission(allowed_roles=_EDIT_ROLES)
def post(self, context: RequestContext):
source: BinaryIO | None = None
if request.mimetype == "multipart/form-data":
uploaded = request.files.get("file")
if uploaded is None or not uploaded.filename:
raise InvalidRosterAgentPackageError("App package file is required")
if not uploaded.filename.lower().endswith(".ifpkg"):
raise InvalidRosterAgentPackageError("App package file must use the .ifpkg extension")
source = cast(BinaryIO, uploaded.stream)
payload = AppImportPayload.model_validate({**request.form.to_dict(), "mode": "yaml-content"})
else:
payload = validate_request(AppImportPayload)
try:
result = application_services().apps.console.import_app(context, payload, source=source)
except NoPermissionError as exc:
raise Forbidden(str(exc)) from exc
except RecommendedAppNotFoundError:
raise RecommendedAppNotFoundHttpError() from None
status_code = {ImportStatus.FAILED: HTTPStatus.BAD_REQUEST, ImportStatus.PENDING: HTTPStatus.ACCEPTED}.get(
result.status, HTTPStatus.OK
)
return result.model_dump(mode="json"), status_code
@console_ns.route("/apps/imports/<string:import_id>/confirm")
class AppImportConfirmApi(Resource):
@console_ns.response(HTTPStatus.OK, "Import confirmed", console_ns.models[Import.__name__])
@console_ns.response(HTTPStatus.BAD_REQUEST, "Import failed", console_ns.models[Import.__name__])
@console_ns.response(HTTPStatus.CONFLICT, "The draft changed after the export the import is based on")
@console_account_admission(
allowed_roles=_EDIT_ROLES,
rbac_checks=[RBACCheck(RBACPermission.APP_IMPORT_EXPORT_DSL, Workspace())],
)
def post(self, context: RequestContext, import_id: str):
try:
result = application_services().apps.console.confirm_import(context, import_id)
except NoPermissionError as exc:
raise Forbidden(str(exc)) from exc
except DraftChangedError as exc:
raise DraftWorkflowNotSync() from exc
status_code = HTTPStatus.BAD_REQUEST if result.status == ImportStatus.FAILED else HTTPStatus.OK
return result.model_dump(mode="json"), status_code
@console_ns.route("/apps/imports/<string:app_id>/check-dependencies")
class AppImportCheckDependenciesApi(Resource):
@error_responses(AppNotFoundError)
@console_ns.response(
HTTPStatus.OK,
"Dependencies checked",
console_ns.models[CheckDependenciesResult.__name__],
)
@console_account_admission(
allowed_roles=_EDIT_ROLES,
rbac_checks=[RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp())],
)
def get(self, context: RequestContext, app_id: str):
try:
result = application_services().apps.console.check_import_dependencies(context, app_id)
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError() from exc
return result.model_dump(mode="json"), HTTPStatus.OK
@@ -0,0 +1,520 @@
import logging
from uuid import UUID
from flask import request
from flask_restx import Resource
from pydantic import BaseModel, ConfigDict, Field, RootModel
from sqlalchemy.orm import Session
from werkzeug.datastructures import FileStorage
from werkzeug.exceptions import HTTPException, InternalServerError
import services
from controllers.common.fields import AudioTranscriptResponse
from controllers.common.rbac import AgentId, PlainApp, RBACCheck, enforce_rbac_checks
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.agent.app_helpers import (
resolve_agent_runtime_app_model,
resolve_existing_agent_runtime_app_model,
)
from controllers.console.app.error import (
AppUnavailableError,
AudioTooLargeError,
CompletionRequestError,
NoAudioUploadedError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderNotSupportSpeechToTextError,
ProviderNotSupportTextToSpeechError,
ProviderQuotaExceededError,
SpeechToTextDisabledError,
UnsupportedAudioTypeError,
)
from controllers.console.app.wraps import get_app_model, with_session
from controllers.console.wraps import (
RBACPermission,
account_initialization_required,
edit_permission_required,
model_validate,
rbac_permission_required,
setup_required,
with_current_tenant_id,
with_current_user,
)
from core.base.tts.audio_mime import SUPPORTED_TTS_AUDIO_MIME_TYPES
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
from extensions.ext_database import db
from fields.base import ResponseModel
from graphon.model_runtime.errors.invoke import InvokeError
from libs.external_api import error_responses
from libs.flask_restx_compat import BINARY_RESPONSE_MEDIA_TYPES_VENDOR_KEY
from libs.helper import dump_response
from libs.login import current_user, login_required
from models import Account, App, AppMode
from models.agent import AgentConfigDraftType
from models.agent_config_entities import AgentSoulConfig
from services.agent.composer_service import AgentComposerService
from services.agent.errors import AgentNotFoundError, AgentVersionNotFoundError
from services.app_ref_service import AppRefService
from services.audio_service import AudioService
from services.errors.audio import (
AudioTooLargeServiceError,
NoAudioUploadedServiceError,
ProviderNotSupportSpeechToTextServiceError,
ProviderNotSupportTextToSpeechServiceError,
SpeechToTextDisabledServiceError,
UnsupportedAudioTypeServiceError,
)
logger = logging.getLogger(__name__)
class TextToSpeechPayload(BaseModel):
message_id: str | None = Field(default=None, description="Message ID")
text: str = Field(..., description="Text to convert")
voice: str | None = Field(default=None, description="Voice name")
streaming: bool | None = Field(default=None, description="Whether to stream audio")
class TextToSpeechVoiceQuery(BaseModel):
language: str = Field(..., description="Language code")
class AgentAudioTranscriptFormPayload(BaseModel):
model_config = ConfigDict(extra="forbid")
draft_type: AgentConfigDraftType = AgentConfigDraftType.DRAFT
class TextToSpeechVoiceResponse(ResponseModel):
# see api/core/plugin/impl/model.py
name: str = Field(description="Voice display name")
value: str = Field(description="Voice identifier")
class TextToSpeechVoiceListResponse(RootModel[list[TextToSpeechVoiceResponse]]):
root: list[TextToSpeechVoiceResponse] = Field(description="Available voices")
register_schema_models(console_ns, AgentAudioTranscriptFormPayload, TextToSpeechPayload, TextToSpeechVoiceQuery)
register_response_schema_models(
console_ns,
AudioTranscriptResponse,
TextToSpeechVoiceResponse,
TextToSpeechVoiceListResponse,
)
_AUDIO_TRANSCRIPT_FILE_PARAM = {
"description": "MP3 audio to transcribe",
"in": "formData",
"type": "file",
"required": True,
}
_AGENT_AUDIO_TRANSCRIPT_PARAMS = {
"file": _AUDIO_TRANSCRIPT_FILE_PARAM,
"draft_type": {
"description": "Agent debug config source",
"in": "formData",
"type": "string",
"enum": [draft_type.value for draft_type in AgentConfigDraftType],
"default": AgentConfigDraftType.DRAFT.value,
"required": False,
},
}
_CONSOLE_AUDIO_TRANSCRIPT_APP_MODES = [
AppMode.CHAT,
AppMode.AGENT_CHAT,
AppMode.ADVANCED_CHAT,
AppMode.AGENT,
]
def _transcribe_audio_to_text(
*,
app_model: App,
file: FileStorage | None,
session: Session,
agent_soul: AgentSoulConfig | None = None,
) -> dict[str, str]:
try:
if agent_soul is None:
response = AudioService.transcript_asr(
app_model=app_model,
file=file,
session=session,
end_user=None,
)
else:
response = AudioService.transcript_agent_asr(
app_model=app_model,
agent_soul=agent_soul,
file=file,
session=session,
end_user=None,
)
return dump_response(AudioTranscriptResponse, response)
except services.errors.app_model_config.AppModelConfigBrokenError as caught_error:
logger.exception("App model config broken.")
raise AppUnavailableError() from caught_error
except NoAudioUploadedServiceError as caught_error:
raise NoAudioUploadedError() from caught_error
except AudioTooLargeServiceError as e:
raise AudioTooLargeError(str(e)) from e
except UnsupportedAudioTypeServiceError as caught_error:
raise UnsupportedAudioTypeError() from caught_error
except ProviderNotSupportSpeechToTextServiceError as caught_error:
raise ProviderNotSupportSpeechToTextError() from caught_error
except SpeechToTextDisabledServiceError as caught_error:
raise SpeechToTextDisabledError() from caught_error
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
except HTTPException:
raise
except ValueError:
raise
except Exception as e:
logger.exception("Failed to transcribe audio to text")
raise InternalServerError() from e
@console_ns.route("/apps/<uuid:app_id>/audio-to-text")
class ChatMessageAudioApi(Resource):
@error_responses(
AgentVersionNotFoundError,
AppUnavailableError,
AudioTooLargeError,
CompletionRequestError,
NoAudioUploadedError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderNotSupportSpeechToTextError,
ProviderQuotaExceededError,
SpeechToTextDisabledError,
UnsupportedAudioTypeError,
)
@console_ns.doc("chat_message_audio_transcript")
@console_ns.doc(description="Transcript audio to text for chat messages")
@console_ns.doc(
consumes=["multipart/form-data"],
params={"app_id": "App ID", "file": _AUDIO_TRANSCRIPT_FILE_PARAM},
)
@console_ns.response(
200,
"Audio transcription successful",
console_ns.models[AudioTranscriptResponse.__name__],
)
@console_ns.response(400, "Bad request - No audio uploaded or unsupported type")
@console_ns.response(413, "Audio file too large")
@setup_required
@login_required
@account_initialization_required
@get_app_model(mode=_CONSOLE_AUDIO_TRANSCRIPT_APP_MODES)
def post(self, app_model: App):
return _transcribe_audio_to_text(
app_model=app_model,
file=request.files.get("file"),
session=db.session(),
)
@console_ns.route("/agent/<uuid:agent_id>/audio-to-text")
class AgentChatMessageAudioApi(Resource):
@error_responses(
AgentNotFoundError,
AgentVersionNotFoundError,
AppUnavailableError,
AudioTooLargeError,
CompletionRequestError,
NoAudioUploadedError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderNotSupportSpeechToTextError,
ProviderQuotaExceededError,
SpeechToTextDisabledError,
UnsupportedAudioTypeError,
)
@console_ns.doc("agent_chat_message_audio_transcript")
@console_ns.doc(description="Transcribe audio using the current Agent debug configuration")
@console_ns.doc(
consumes=["multipart/form-data"],
params={"agent_id": "Agent ID", **_AGENT_AUDIO_TRANSCRIPT_PARAMS},
)
@console_ns.response(
200,
"Audio transcription successful",
console_ns.models[AudioTranscriptResponse.__name__],
)
@console_ns.response(400, "Bad request - Speech to text disabled or unsupported audio")
@console_ns.response(404, "Agent or build draft not found")
@console_ns.response(413, "Audio file too large")
@setup_required
@login_required
@account_initialization_required
@edit_permission_required
@with_current_user
@with_current_tenant_id
@with_session
def post(
self,
session: Session,
current_tenant_id: str,
current_user: Account,
agent_id: UUID,
):
payload = AgentAudioTranscriptFormPayload.model_validate(request.form.to_dict(flat=True))
app_model = resolve_agent_runtime_app_model(
session=session,
tenant_id=current_tenant_id,
agent_id=agent_id,
)
# Agent routes expose Agent ids, while APP RBAC is keyed by the resolved runtime App id.
enforce_rbac_checks(
tenant_id=current_tenant_id,
account_id=current_user.id,
checks=[RBACCheck(RBACPermission.APP_TEST_AND_RUN, PlainApp())],
path_args={"app_id": app_model.id},
)
agent_soul = AgentComposerService.load_agent_soul_for_debug(
tenant_id=current_tenant_id,
agent_id=str(agent_id),
account_id=current_user.id,
draft_type=payload.draft_type,
session=session,
)
return _transcribe_audio_to_text(
app_model=app_model,
agent_soul=agent_soul,
file=request.files.get("file"),
session=session,
)
def _text_to_speech(*, payload: TextToSpeechPayload, app_model: App, session: Session, account_id: str | None):
try:
message_ref = None
if payload.message_id:
app_ref = AppRefService.create_app_ref(app_model)
message_ref = AppRefService.create_message_ref(
app_ref,
payload.message_id,
account_id=account_id,
)
return AudioService.transcript_tts(
app_model=app_model,
session=session,
text=payload.text,
voice=payload.voice,
message_ref=message_ref,
is_draft=True,
)
except services.errors.app_model_config.AppModelConfigBrokenError as caught_error:
logger.exception("App model config broken.")
raise AppUnavailableError() from caught_error
except ProviderNotSupportTextToSpeechServiceError as caught_error:
raise ProviderNotSupportTextToSpeechError() from caught_error
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
except HTTPException:
raise
except ValueError:
raise
except Exception as e:
logger.exception("Failed to convert text to speech")
raise InternalServerError() from e
@console_ns.route("/apps/<uuid:app_id>/text-to-audio")
class ChatMessageTextApi(Resource):
@error_responses(
AppUnavailableError,
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderNotSupportTextToSpeechError,
ProviderQuotaExceededError,
)
@console_ns.doc("chat_message_text_to_speech")
@console_ns.doc(description="Convert text to speech for chat messages")
@console_ns.doc(params={"app_id": "App ID"})
@console_ns.expect(console_ns.models[TextToSpeechPayload.__name__])
# TTS returns provider audio bytes, so the success response is intentionally schema-less.
@console_ns.response(200, "Text to speech conversion successful")
@console_ns.response(400, "Bad request - Invalid parameters")
@setup_required
@login_required
@account_initialization_required
@get_app_model
@model_validate(TextToSpeechPayload)
def post(self, req_data: TextToSpeechPayload, app_model: App):
# response-contract:ignore provider audio bytes
return _text_to_speech(
payload=req_data,
app_model=app_model,
session=db.session(),
account_id=current_user.id if req_data.message_id else None,
)
@console_ns.route("/agent/<uuid:agent_id>/text-to-audio")
class AgentChatMessageTextApi(Resource):
@error_responses(
AgentNotFoundError,
AppUnavailableError,
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderNotSupportTextToSpeechError,
ProviderQuotaExceededError,
)
@console_ns.doc("agent_chat_message_text_to_speech")
@console_ns.doc(description="Preview an Agent TTS voice or read an Agent chat message")
@console_ns.doc(params={"agent_id": "Agent ID"})
@console_ns.doc(
produces=list(SUPPORTED_TTS_AUDIO_MIME_TYPES),
vendor={BINARY_RESPONSE_MEDIA_TYPES_VENDOR_KEY: list(SUPPORTED_TTS_AUDIO_MIME_TYPES)},
)
@console_ns.expect(console_ns.models[TextToSpeechPayload.__name__])
@console_ns.response(200, "Generated audio bytes in the provider audio format")
@console_ns.response(400, "Invalid text or voice")
@console_ns.response(403, "Insufficient permissions")
@console_ns.response(404, "Agent not found")
@setup_required
@login_required
@account_initialization_required
@edit_permission_required
@with_current_user
@with_current_tenant_id
@with_session(write=False)
@model_validate(TextToSpeechPayload)
def post(
self,
req_data: TextToSpeechPayload,
session: Session,
current_tenant_id: str,
current_user: Account,
agent_id: UUID,
):
enforce_rbac_checks(
tenant_id=current_tenant_id,
account_id=current_user.id,
checks=[RBACCheck(RBACPermission.AGENT_TEST_AND_RUN, AgentId())],
path_args={"agent_id": str(agent_id)},
)
app_model = resolve_existing_agent_runtime_app_model(
session=session,
tenant_id=current_tenant_id,
agent_id=agent_id,
)
# response-contract:ignore provider audio bytes
return _text_to_speech(payload=req_data, app_model=app_model, session=session, account_id=current_user.id)
def _get_text_to_speech_voices(*, tenant_id: str, language: str):
try:
response = AudioService.transcript_tts_voices(
tenant_id=tenant_id,
language=language,
)
return dump_response(TextToSpeechVoiceListResponse, response)
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
except ValueError as e:
raise e
except Exception as e:
logger.exception("Failed to handle get request to TextModesApi")
raise InternalServerError() from e
@console_ns.route("/apps/<uuid:app_id>/text-to-audio/voices")
class TextModesApi(Resource):
@error_responses(
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("get_text_to_speech_voices")
@console_ns.doc(description="Get available TTS voices for a specific language")
@console_ns.doc(params={"app_id": "App ID", **query_params_from_model(TextToSpeechVoiceQuery)})
@console_ns.response(
200,
"TTS voices retrieved successfully",
console_ns.models[TextToSpeechVoiceListResponse.__name__],
)
@console_ns.response(400, "Invalid language parameter")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model
@model_validate(TextToSpeechVoiceQuery)
def get(self, req_data: TextToSpeechVoiceQuery, app_model: App):
return _get_text_to_speech_voices(tenant_id=app_model.tenant_id, language=req_data.language)
@console_ns.route("/agent/<uuid:agent_id>/text-to-audio/voices")
class AgentTextToSpeechVoicesApi(Resource):
@error_responses(
AgentNotFoundError,
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("get_agent_text_to_speech_voices")
@console_ns.doc(description="Get available TTS voices for an Agent and language")
@console_ns.doc(params={"agent_id": "Agent ID", **query_params_from_model(TextToSpeechVoiceQuery)})
@console_ns.response(
200,
"TTS voices retrieved successfully",
console_ns.models[TextToSpeechVoiceListResponse.__name__],
)
@console_ns.response(400, "Invalid language parameter")
@console_ns.response(404, "Agent not found")
@setup_required
@login_required
@account_initialization_required
@with_current_user
@with_current_tenant_id
@with_session(write=False)
@model_validate(TextToSpeechVoiceQuery)
def get(
self,
req_data: TextToSpeechVoiceQuery,
session: Session,
current_tenant_id: str,
current_user: Account,
agent_id: UUID,
):
enforce_rbac_checks(
tenant_id=current_tenant_id,
account_id=current_user.id,
checks=[RBACCheck(RBACPermission.AGENT_PREVIEW, AgentId())],
path_args={"agent_id": str(agent_id)},
)
app_model = resolve_existing_agent_runtime_app_model(
session=session,
tenant_id=current_tenant_id,
agent_id=agent_id,
)
return _get_text_to_speech_voices(tenant_id=app_model.tenant_id, language=req_data.language)
@@ -0,0 +1,692 @@
import json
import logging
from collections.abc import Generator, Iterator, Mapping
from typing import Any, Literal, Protocol, runtime_checkable
from uuid import UUID
from flask import request
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from sqlalchemy.orm import Session
from werkzeug.exceptions import BadRequest, InternalServerError, NotFound
import services
from controllers.common.fields import SimpleResultResponse
from controllers.common.rbac import AgentId, PlainApp, RBACCheck
from controllers.common.schema import register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.agent.app_helpers import resolve_agent_runtime_app_model
from controllers.console.app.error import (
AppUnavailableError,
CompletionRequestError,
ConversationCompletedError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
from controllers.console.app.wraps import get_app_model, with_session
from controllers.console.wraps import (
RBACPermission,
account_initialization_required,
edit_permission_required,
model_validate,
rbac_permission_required,
setup_required,
with_current_tenant_id,
with_current_user,
with_current_user_id,
)
from controllers.web.error import InvokeRateLimitError as InvokeRateLimitHttpError
from core.app.entities.app_invoke_entities import InvokeFrom
from core.app.features.rate_limiting.rate_limit import RateLimitGenerator
from core.errors.error import (
ModelCurrentlyNotSupportError,
ProviderTokenNotInitError,
QuotaExceededError,
)
from core.helper.trace_id_helper import get_external_trace_id
from graphon.model_runtime.errors.invoke import InvokeError
from libs import helper
from libs.external_api import error_responses
from libs.helper import uuid_value
from libs.login import login_required
from models import Account
from models.agent import AgentConfigDraftType
from models.model import App, AppMode
from services.agent.errors import AgentNotFoundError
from services.agent.roster_service import AgentRosterService
from services.app_generate_service import AppGenerateService
from services.app_task_service import AppTaskService
from services.errors.llm import InvokeRateLimitError
logger = logging.getLogger(__name__)
@runtime_checkable
class _ClosableStream(Protocol):
def close(self) -> None: ...
def _resolve_debugger_chat_streaming(
*, app_mode: AppMode, response_mode: str, response_mode_provided: bool = True
) -> bool:
"""Agent App runtime is SSE-only until backend blocking runs are supported."""
if app_mode != AppMode.AGENT:
return response_mode != "blocking"
if response_mode_provided and response_mode == "blocking":
raise BadRequest("Agent App only supports streaming response mode.")
return True
class BaseMessagePayload(BaseModel):
inputs: dict[str, Any]
# Agent Apps (AppMode.AGENT) derive their model + prompt from the bound Agent
# Soul, so no override ``model_config`` is sent; chat / agent-chat / completion
# debugging still pass it. Optional here, required in practice by those modes
# downstream when their config is built from args.
model_config_data: dict[str, Any] = Field(
default_factory=dict,
alias="model_config",
)
files: list[Any] | None = Field(
default=None,
description="Uploaded files",
)
response_mode: Literal["blocking", "streaming"] = Field(default="blocking", description="Response mode")
retriever_from: str = Field(default="dev", description="Retriever source")
class CompletionMessagePayload(BaseMessagePayload):
query: str = Field(default="", description="Query text")
class ChatMessagePayload(BaseMessagePayload):
query: str = Field(..., description="User query")
conversation_id: str | None = Field(default=None, description="Conversation ID")
parent_message_id: str | None = Field(default=None, description="Parent message ID")
draft_type: Literal["draft", "debug_build"] = Field(
default="draft",
description="Agent App debug config source. Use debug_build while the Agent is in build mode.",
)
@field_validator("conversation_id", "parent_message_id")
@classmethod
def validate_uuid(cls, value: str | None) -> str | None:
if value is None:
return value
return uuid_value(value)
_BUILD_CHAT_FINALIZATION_QUERY = """Finalize this Build chat configuration for the agent.
This step is only for persisting Agent config changes discovered in the current Build chat. Do not install packages,
edit workspace files, run validation or debugging commands, make exploratory checks, or perform other work.
Use only the current Build chat message history to identify changes that need to be persisted. Do not inspect, test, or
validate old config unless the message history already shows that the old config is invalid.
Only update the build-draft config note when the current Build chat contains durable context that later runs need.
Write the config note in the language used by the message history.
Do not create, update, delete, inspect, or fill gaps in other Agent config resources, including config files, config
skills, config env, tools, models, knowledge, or prompt settings.
When updating the config note with the Agent config CLI usage provided in the runtime prompt, record only durable
context needed by later runs, such as:
- what you installed or configured outside the workspace for this agent,
- where those external updates live, including CLI tools, packages, and persistent $HOME paths,
- how the agent should use it in later runs,
- any setup, authentication, or user action still required.
After config persistence completes, respond FINISHED."""
register_schema_models(console_ns, CompletionMessagePayload, ChatMessagePayload)
register_response_schema_models(console_ns, SimpleResultResponse)
# define completion message api for user
@console_ns.route("/apps/<uuid:app_id>/completion-messages")
class CompletionMessageApi(Resource):
@error_responses(
AppUnavailableError,
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("create_completion_message")
@console_ns.doc(description="Generate completion message for debugging")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[CompletionMessagePayload.__name__])
@console_ns.response(200, "Completion generated successfully")
@console_ns.response(400, "Invalid request parameters")
@console_ns.response(404, "App not found")
@setup_required
@login_required
@account_initialization_required
@with_current_user
@rbac_permission_required(RBACCheck(RBACPermission.APP_TEST_AND_RUN, PlainApp()))
@with_session
@get_app_model(mode=AppMode.COMPLETION)
@model_validate(CompletionMessagePayload)
def post(self, req_data: CompletionMessagePayload, session: Session, current_user: Account, app_model: App):
args = req_data.model_dump(exclude_none=True, by_alias=True)
streaming = req_data.response_mode != "blocking"
args["auto_generate_name"] = False
try:
response = AppGenerateService.generate(
session=session,
app_model=app_model,
user=current_user,
args=args,
invoke_from=InvokeFrom.DEBUGGER,
streaming=streaming,
)
# response-contract:ignore compact_generate_response
return helper.compact_generate_response(response)
except services.errors.conversation.ConversationNotExistsError as caught_error:
raise NotFound("Conversation Not Exists.") from caught_error
except services.errors.conversation.ConversationCompletedError as caught_error:
raise ConversationCompletedError() from caught_error
except services.errors.app_model_config.AppModelConfigBrokenError as caught_error:
logger.exception("App model config broken.")
raise AppUnavailableError() from caught_error
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
except ValueError as e:
raise e
except Exception as e:
logger.exception("internal server error.")
raise InternalServerError() from e
@console_ns.route("/apps/<uuid:app_id>/completion-messages/<string:task_id>/stop")
class CompletionMessageStopApi(Resource):
@console_ns.doc("stop_completion_message")
@console_ns.doc(description="Stop a running completion message generation")
@console_ns.doc(params={"app_id": "Application ID", "task_id": "Task ID to stop"})
@console_ns.response(200, "Task stopped successfully", console_ns.models[SimpleResultResponse.__name__])
@setup_required
@login_required
@account_initialization_required
@with_current_user_id
@get_app_model(mode=AppMode.COMPLETION)
def post(self, current_user_id: str, app_model: App, task_id: str):
AppTaskService.stop_task(
tenant_id=app_model.tenant_id,
app_id=app_model.id,
task_id=task_id,
invoke_from=InvokeFrom.DEBUGGER,
user_id=current_user_id,
app_mode=AppMode.value_of(app_model.mode),
)
return SimpleResultResponse(result="success").model_dump(mode="json"), 200
@console_ns.route("/apps/<uuid:app_id>/chat-messages")
class ChatMessageApi(Resource):
@error_responses(
AgentNotFoundError,
AppUnavailableError,
CompletionRequestError,
InvokeRateLimitHttpError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("create_chat_message")
@console_ns.doc(description="Generate chat message for debugging")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[ChatMessagePayload.__name__])
@console_ns.response(200, "Chat message generated successfully")
@console_ns.response(400, "Invalid request parameters")
@console_ns.response(404, "App or conversation not found")
@setup_required
@login_required
@account_initialization_required
@edit_permission_required
@with_current_user
@with_current_tenant_id
@rbac_permission_required(RBACCheck(RBACPermission.APP_TEST_AND_RUN, PlainApp()))
@with_session
@get_app_model(mode=[AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.AGENT])
def post(self, session: Session, current_tenant_id: str, current_user: Account, app_model: App):
return _create_chat_message(
session=session, current_tenant_id=current_tenant_id, current_user=current_user, app_model=app_model
)
@console_ns.route("/agent/<uuid:agent_id>/chat-messages")
class AgentChatMessageApi(Resource):
@error_responses(
AgentNotFoundError,
AppUnavailableError,
CompletionRequestError,
InvokeRateLimitHttpError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("create_agent_chat_message")
@console_ns.doc(description="Generate an Agent App chat message for debugging")
@console_ns.doc(params={"agent_id": "Agent ID"})
@console_ns.expect(console_ns.models[ChatMessagePayload.__name__])
@console_ns.response(200, "Chat message generated successfully")
@console_ns.response(400, "Invalid request parameters")
@console_ns.response(404, "Agent or conversation not found")
@setup_required
@login_required
@account_initialization_required
@edit_permission_required
@rbac_permission_required(RBACCheck(RBACPermission.AGENT_TEST_AND_RUN, AgentId()))
@with_current_user
@with_current_tenant_id
@with_session
def post(self, session: Session, current_tenant_id: str, current_user: Account, agent_id: UUID):
app_model = AgentRosterService(session).get_agent_runtime_app_model(
tenant_id=current_tenant_id, agent_id=str(agent_id)
)
return _create_chat_message(
session=session,
current_tenant_id=current_tenant_id,
current_user=current_user,
app_model=app_model,
agent_id=str(agent_id),
)
@console_ns.route("/agent/<uuid:agent_id>/build-chat/finalize")
class AgentBuildChatFinalizeApi(Resource):
@error_responses(
AgentNotFoundError,
AppUnavailableError,
CompletionRequestError,
InvokeRateLimitHttpError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("finalize_agent_build_chat")
@console_ns.doc(description="Run a build-draft Agent App turn that asks the agent to push config updates")
@console_ns.doc(params={"agent_id": "Agent ID"})
@console_ns.response(200, "Success", console_ns.models[SimpleResultResponse.__name__])
@console_ns.response(400, "Invalid request parameters")
@console_ns.response(404, "Agent, build draft, or conversation not found")
@setup_required
@login_required
@account_initialization_required
@edit_permission_required
@rbac_permission_required(RBACCheck(RBACPermission.AGENT_TEST_AND_RUN, AgentId()))
@with_current_user
@with_current_tenant_id
@with_session
def post(self, session: Session, current_tenant_id: str, current_user: Account, agent_id: UUID):
app_model = AgentRosterService(session).get_agent_runtime_app_model(
tenant_id=current_tenant_id, agent_id=str(agent_id)
)
return _create_build_chat_finalization_message(
session=session,
current_tenant_id=current_tenant_id,
current_user=current_user,
app_model=app_model,
agent_id=str(agent_id),
)
@console_ns.route("/apps/<uuid:app_id>/chat-messages/<string:task_id>/stop")
class ChatMessageStopApi(Resource):
@console_ns.doc("stop_chat_message")
@console_ns.doc(description="Stop a running chat message generation")
@console_ns.doc(params={"app_id": "Application ID", "task_id": "Task ID to stop"})
@console_ns.response(200, "Task stopped successfully", console_ns.models[SimpleResultResponse.__name__])
@setup_required
@login_required
@account_initialization_required
@with_current_user_id
@get_app_model(mode=[AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def post(self, current_user_id: str, app_model: App, task_id: str):
return _stop_chat_message(current_user_id=current_user_id, app_model=app_model, task_id=task_id)
@console_ns.route("/agent/<uuid:agent_id>/chat-messages/<string:task_id>/stop")
class AgentChatMessageStopApi(Resource):
@error_responses(AgentNotFoundError)
@console_ns.doc("stop_agent_chat_message")
@console_ns.doc(description="Stop a running Agent App chat message generation")
@console_ns.doc(params={"agent_id": "Agent ID", "task_id": "Task ID to stop"})
@console_ns.response(200, "Task stopped successfully", console_ns.models[SimpleResultResponse.__name__])
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.AGENT_TEST_AND_RUN, AgentId()))
@with_current_user_id
@with_current_tenant_id
@with_session(write=False)
def post(self, session: Session, current_tenant_id: str, current_user_id: str, agent_id: UUID, task_id: str):
app_model = resolve_agent_runtime_app_model(
session=session,
tenant_id=current_tenant_id,
agent_id=agent_id,
)
return _stop_chat_message(current_user_id=current_user_id, app_model=app_model, task_id=task_id)
def _resolve_current_user_agent_debug_conversation_id(
*,
session: Session,
current_tenant_id: str,
current_user: Account,
app_model: App,
agent_id: str | None,
draft_type: AgentConfigDraftType,
start_new: bool = False,
) -> str:
"""Resolve the current editor's Build or Preview conversation."""
roster_service = AgentRosterService(session)
resolved_agent_id = agent_id
if not resolved_agent_id:
agent = roster_service.get_app_backing_agent(tenant_id=current_tenant_id, app_id=str(app_model.id))
if agent is None:
raise AgentNotFoundError()
resolved_agent_id = agent.id
if draft_type == AgentConfigDraftType.DEBUG_BUILD:
return roster_service.get_or_create_build_conversation(
tenant_id=current_tenant_id,
agent_id=resolved_agent_id,
account_id=current_user.id,
)
if start_new:
return roster_service.rotate_preview_conversation(
tenant_id=current_tenant_id,
agent_id=resolved_agent_id,
account_id=current_user.id,
)
conversation_id = roster_service.get_current_preview_conversation(
tenant_id=current_tenant_id,
agent_id=resolved_agent_id,
account_id=current_user.id,
)
if conversation_id is None:
raise NotFound("Conversation Not Exists.")
return conversation_id
def _create_chat_message(
*,
session: Session,
current_user: Account,
app_model: App,
current_tenant_id: str | None = None,
agent_id: str | None = None,
):
raw_payload = console_ns.payload or {}
args_model = ChatMessagePayload.model_validate(raw_payload)
args = args_model.model_dump(exclude_none=True, by_alias=True)
if AppMode.value_of(app_model.mode) == AppMode.AGENT:
draft_type = AgentConfigDraftType(args_model.draft_type)
# Preview follows the normal chat contract: an omitted/empty conversation ID starts a new
# conversation. Build chat keeps its stable mapping so build drafts and finalization stay continuous.
debug_conversation_id = _resolve_current_user_agent_debug_conversation_id(
session=session,
current_tenant_id=current_tenant_id or app_model.tenant_id,
current_user=current_user,
app_model=app_model,
agent_id=agent_id,
draft_type=draft_type,
start_new=draft_type == AgentConfigDraftType.DRAFT and not args_model.conversation_id,
)
if args_model.conversation_id and args_model.conversation_id != debug_conversation_id:
raise NotFound("Conversation Not Exists.")
args["conversation_id"] = debug_conversation_id
streaming = _resolve_debugger_chat_streaming(
app_mode=AppMode.value_of(app_model.mode),
response_mode=args_model.response_mode,
response_mode_provided=isinstance(raw_payload, dict) and "response_mode" in raw_payload,
)
if AppMode.value_of(app_model.mode) == AppMode.AGENT:
args["response_mode"] = "streaming"
args["auto_generate_name"] = False
external_trace_id = get_external_trace_id(request)
if external_trace_id:
args["external_trace_id"] = external_trace_id
return _generate_chat_message_response(
session=session,
current_user=current_user,
app_model=app_model,
args=args,
streaming=streaming,
)
def _create_build_chat_finalization_message(
*, session: Session, current_user: Account, app_model: App, current_tenant_id: str, agent_id: str
):
debug_conversation_id = _resolve_current_user_agent_debug_conversation_id(
session=session,
current_tenant_id=current_tenant_id,
current_user=current_user,
app_model=app_model,
agent_id=agent_id,
draft_type=AgentConfigDraftType.DEBUG_BUILD,
)
args: dict[str, Any] = {
"query": _BUILD_CHAT_FINALIZATION_QUERY,
"inputs": {},
"response_mode": "streaming",
"draft_type": "debug_build",
"conversation_id": debug_conversation_id,
"auto_generate_name": False,
}
external_trace_id = get_external_trace_id(request)
if external_trace_id:
args["external_trace_id"] = external_trace_id
response = _generate_chat_message(
session=session,
current_user=current_user,
app_model=app_model,
args=args,
streaming=True,
)
_drain_streaming_generate_response(response)
return {"result": "success"}, 200
def _drain_streaming_generate_response(response: RateLimitGenerator | Generator[str]) -> None:
"""Consume a streamed app-generate response until a terminal message event arrives.
Finalize keeps the normal Agent App streaming path so the existing queue,
persistence, and runtime-session behavior stay intact. The console API only
changes the HTTP boundary: it drains the SSE stream server-side and returns
success after the generated build-chat message reaches ``message_end``.
"""
try:
for chunk in response:
for raw_event in chunk.split("\n\n"):
if not raw_event.strip():
continue
event_name: str | None = None
data_lines: list[str] = []
for line in raw_event.splitlines():
if line.startswith("event: "):
event_name = line.removeprefix("event: ").strip()
elif line.startswith("data: "):
data_lines.append(line.removeprefix("data: "))
if not data_lines:
if event_name == "ping":
continue
continue
payload = json.loads("\n".join(data_lines))
if not isinstance(payload, dict):
continue
payload_event = payload.get("event")
if payload_event == "message_end":
return
if payload_event == "error":
raise CompletionRequestError(str(payload.get("message") or "Build chat finalization failed."))
finally:
if isinstance(response, _ClosableStream):
response.close()
raise CompletionRequestError("Build chat finalization did not complete.")
def _generate_chat_message(
*,
session: Session,
current_user: Account,
app_model: App,
args: dict[str, Any],
streaming: bool,
):
try:
return AppGenerateService.generate(
session=session,
app_model=app_model,
user=current_user,
args=args,
invoke_from=InvokeFrom.DEBUGGER,
streaming=streaming,
)
except services.errors.conversation.ConversationNotExistsError as caught_error:
raise NotFound("Conversation Not Exists.") from caught_error
except services.errors.conversation.ConversationCompletedError as caught_error:
raise ConversationCompletedError() from caught_error
except services.errors.app_model_config.AppModelConfigBrokenError as caught_error:
logger.exception("App model config broken.")
raise AppUnavailableError() from caught_error
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeRateLimitError as ex:
raise InvokeRateLimitHttpError(ex.description) from ex
except CompletionRequestError:
raise
except InvokeError as e:
raise CompletionRequestError(e.description) from e
except ValueError as e:
raise e
except Exception as e:
logger.exception("internal server error.")
raise InternalServerError() from e
def _generate_chat_message_response(
*,
session: Session,
current_user: Account,
app_model: App,
args: dict[str, Any],
streaming: bool,
):
response = _generate_chat_message(
session=session,
current_user=current_user,
app_model=app_model,
args=args,
streaming=streaming,
)
if AppMode.value_of(app_model.mode) == AppMode.AGENT and streaming:
response = _raise_agent_stream_error_before_response(response)
return helper.compact_generate_response(response)
def _stop_chat_message(*, current_user_id: str, app_model: App, task_id: str):
AppTaskService.stop_task(
tenant_id=app_model.tenant_id,
app_id=app_model.id,
task_id=task_id,
invoke_from=InvokeFrom.DEBUGGER,
user_id=current_user_id,
app_mode=AppMode.value_of(app_model.mode),
)
return SimpleResultResponse(result="success").model_dump(mode="json"), 200
def _raise_agent_stream_error_before_response(response):
"""Surface immediate Agent App stream errors as HTTP errors before SSE starts.
The shared streaming helper always returns HTTP 200 once the SSE response is
created. Agent v2 configuration errors, such as an invalid model API key,
can be the first real stream event after the initial ping; pre-reading that
first non-ping event lets the console API return the existing 400 error
contract instead of a successful HTTP response carrying only an SSE error.
"""
if isinstance(response, Mapping):
return response
buffered: list[str] = []
iterator = iter(response)
while True:
try:
chunk = next(iterator)
except StopIteration:
return iter(buffered)
if not isinstance(chunk, str):
return _prepend_stream_chunks(buffered, chunk, iterator)
if _is_sse_ping(chunk):
buffered.append(chunk)
continue
error_payload = _extract_sse_error_payload(chunk)
if error_payload is not None:
if isinstance(response, _ClosableStream):
response.close()
message = error_payload.get("message")
raise CompletionRequestError(str(message or "Agent App chat failed."))
return _prepend_stream_chunks(buffered, chunk, iterator)
def _prepend_stream_chunks(buffered: list[Any], first: Any, iterator: Iterator[Any]) -> Generator[Any]:
yield from buffered
yield first
yield from iterator
def _is_sse_ping(chunk: str) -> bool:
return chunk.strip() == "event: ping"
def _extract_sse_error_payload(chunk: str) -> dict[str, Any] | None:
for raw_event in chunk.split("\n\n"):
data_lines: list[str] = []
for line in raw_event.splitlines():
if line.startswith("data: "):
data_lines.append(line.removeprefix("data: "))
if not data_lines:
continue
try:
payload = json.loads("\n".join(data_lines))
except json.JSONDecodeError:
continue
if isinstance(payload, dict) and payload.get("event") == "error":
return payload
return None
@@ -0,0 +1,288 @@
from http import HTTPStatus
from typing import Literal
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from controllers.common.errors import ConversationNotFoundHTTPError, InvalidArgumentError, UnauthorizedError
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AppNotFoundError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import RBACPermission, validate_request
from enums.account import TenantAccountRole
from extensions.ext_application_services import application_services
from fields.conversation_fields import (
Conversation as ConversationResponse,
)
from fields.conversation_fields import (
ConversationDetail as ConversationDetailResponse,
)
from fields.conversation_fields import (
ConversationMessageDetail as ConversationMessageDetailResponse,
)
from fields.conversation_fields import (
ConversationPagination as ConversationPaginationResponse,
)
from fields.conversation_fields import (
ConversationWithSummaryPagination as ConversationWithSummaryPaginationResponse,
)
from libs.datetime_utils import parse_time_range
from libs.external_api import error_responses
from libs.helper import dump_response
from machinery.context import RequestContext
from services.app.console_service import ConsoleAppNotFoundError
from services.entities.conversation_entities import ConsoleConversationFilters
from services.errors.base import NoPermissionError
from services.errors.conversation import ConversationNotExistsError
class BaseConversationQuery(BaseModel):
keyword: str | None = Field(default=None, description="Search keyword")
start: str | None = Field(default=None, description="Start date (YYYY-MM-DD HH:MM)")
end: str | None = Field(default=None, description="End date (YYYY-MM-DD HH:MM)")
annotation_status: Literal["annotated", "not_annotated", "all"] = Field(
default="all", description="Annotation status filter"
)
page: int = Field(default=1, ge=1, le=99999, description="Page number")
limit: int = Field(default=20, ge=1, le=100, description="Page size (1-100)")
@field_validator("start", "end", mode="before")
@classmethod
def blank_to_none(cls, value: str | None) -> str | None:
if value == "":
return None
return value
class CompletionConversationQuery(BaseConversationQuery):
pass
class ChatConversationQuery(BaseConversationQuery):
sort_by: Literal["created_at", "-created_at", "updated_at", "-updated_at"] = Field(
default="-updated_at", description="Sort field and direction"
)
register_schema_models(
console_ns,
CompletionConversationQuery,
ChatConversationQuery,
)
register_response_schema_models(
console_ns,
ConversationResponse,
ConversationPaginationResponse,
ConversationMessageDetailResponse,
ConversationWithSummaryPaginationResponse,
ConversationDetailResponse,
)
@console_ns.route("/apps/<uuid:app_id>/completion-conversations")
class CompletionConversationApi(Resource):
@error_responses(AppNotFoundError)
@console_ns.doc("list_completion_conversations")
@console_ns.doc(description="Get completion conversations with pagination and filtering")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(CompletionConversationQuery)})
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[ConversationPaginationResponse.__name__])
@console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid date range")
@console_ns.response(HTTPStatus.UNAUTHORIZED, "Authentication required")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App not found")
@console_ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, "Invalid query parameters")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
def get(self, request_context: RequestContext, app_id: UUID) -> dict[str, object]:
queries = application_services().conversation_queries
try:
scope = queries.get_console_scope(request_context, str(app_id), kind="completion")
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError(str(exc)) from exc
except NoPermissionError as exc:
raise UnauthorizedError(str(exc)) from exc
req_data = validate_request(CompletionConversationQuery)
try:
start, end = parse_time_range(req_data.start, req_data.end, scope.timezone)
except ValueError as exc:
raise InvalidArgumentError(str(exc)) from exc
try:
page = queries.get_console_page(
scope=scope,
filters=ConsoleConversationFilters(
keyword=req_data.keyword,
start=start,
end=end.replace(second=59) if end else None,
annotation_status=req_data.annotation_status,
sort_by="-created_at",
page=req_data.page,
limit=req_data.limit,
),
)
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError(str(exc)) from exc
return dump_response(ConversationPaginationResponse, page)
@console_ns.route("/apps/<uuid:app_id>/completion-conversations/<uuid:conversation_id>")
class CompletionConversationDetailApi(Resource):
@error_responses(AppNotFoundError, ConversationNotFoundHTTPError)
@console_ns.doc("get_completion_conversation")
@console_ns.doc(description="Get completion conversation details with messages")
@console_ns.doc(params={"app_id": "Application ID", "conversation_id": "Conversation ID"})
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[ConversationMessageDetailResponse.__name__])
@console_ns.response(HTTPStatus.UNAUTHORIZED, "Authentication required")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App or conversation not found")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
def get(self, request_context: RequestContext, app_id: UUID, conversation_id: UUID) -> dict[str, object]:
services = application_services()
try:
detail = services.conversation_queries.get_console_detail(
request_context, str(app_id), str(conversation_id), kind="completion"
)
response = dump_response(ConversationMessageDetailResponse, detail)
# Preserve the Console read receipt only after rendering succeeds.
services.conversation_commands.mark_console_read(
request_context, str(app_id), str(conversation_id), kind="completion"
)
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError(str(exc)) from exc
except ConversationNotExistsError as exc:
raise ConversationNotFoundHTTPError() from exc
return response
@error_responses(AppNotFoundError, ConversationNotFoundHTTPError)
@console_ns.doc("delete_completion_conversation")
@console_ns.doc(description="Delete a completion conversation")
@console_ns.doc(params={"app_id": "Application ID", "conversation_id": "Conversation ID"})
@console_ns.response(HTTPStatus.NO_CONTENT, "Conversation deleted successfully")
@console_ns.response(HTTPStatus.UNAUTHORIZED, "Authentication required")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App or conversation not found")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_EDIT, PlainApp()),),
)
def delete(self, request_context: RequestContext, app_id: UUID, conversation_id: UUID) -> tuple[str, HTTPStatus]:
try:
application_services().conversation_commands.delete_console(
request_context, str(app_id), str(conversation_id), kind="completion"
)
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError(str(exc)) from exc
except ConversationNotExistsError as exc:
raise ConversationNotFoundHTTPError() from exc
return "", HTTPStatus.NO_CONTENT
@console_ns.route("/apps/<uuid:app_id>/chat-conversations")
class ChatConversationApi(Resource):
@error_responses(AppNotFoundError)
@console_ns.doc("list_chat_conversations")
@console_ns.doc(description="Get chat conversations with pagination and filtering")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(ChatConversationQuery)})
@console_ns.response(
HTTPStatus.OK, "Success", console_ns.models[ConversationWithSummaryPaginationResponse.__name__]
)
@console_ns.response(HTTPStatus.BAD_REQUEST, "Invalid date range")
@console_ns.response(HTTPStatus.UNAUTHORIZED, "Authentication required")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App not found")
@console_ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, "Invalid query parameters")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
def get(self, request_context: RequestContext, app_id: UUID) -> dict[str, object]:
queries = application_services().conversation_queries
try:
scope = queries.get_console_scope(request_context, str(app_id), kind="chat")
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError(str(exc)) from exc
except NoPermissionError as exc:
raise UnauthorizedError(str(exc)) from exc
req_data = validate_request(ChatConversationQuery)
try:
start, end = parse_time_range(req_data.start, req_data.end, scope.timezone)
except ValueError as exc:
raise InvalidArgumentError(str(exc)) from exc
try:
page = queries.get_console_page(
scope=scope,
filters=ConsoleConversationFilters(
keyword=req_data.keyword,
start=start,
end=end.replace(second=59) if end else None,
annotation_status=req_data.annotation_status,
sort_by=req_data.sort_by,
page=req_data.page,
limit=req_data.limit,
),
)
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError(str(exc)) from exc
return dump_response(ConversationWithSummaryPaginationResponse, page)
@console_ns.route("/apps/<uuid:app_id>/chat-conversations/<uuid:conversation_id>")
class ChatConversationDetailApi(Resource):
@error_responses(AppNotFoundError, ConversationNotFoundHTTPError)
@console_ns.doc("get_chat_conversation")
@console_ns.doc(description="Get chat conversation details")
@console_ns.doc(params={"app_id": "Application ID", "conversation_id": "Conversation ID"})
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[ConversationDetailResponse.__name__])
@console_ns.response(HTTPStatus.UNAUTHORIZED, "Authentication required")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App or conversation not found")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
def get(self, request_context: RequestContext, app_id: UUID, conversation_id: UUID) -> dict[str, object]:
services = application_services()
try:
detail = services.conversation_queries.get_console_detail(
request_context, str(app_id), str(conversation_id), kind="chat"
)
response = dump_response(ConversationDetailResponse, detail)
# Preserve the Console read receipt only after rendering succeeds.
services.conversation_commands.mark_console_read(
request_context, str(app_id), str(conversation_id), kind="chat"
)
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError(str(exc)) from exc
except ConversationNotExistsError as exc:
raise ConversationNotFoundHTTPError() from exc
return response
@error_responses(AppNotFoundError, ConversationNotFoundHTTPError)
@console_ns.doc("delete_chat_conversation")
@console_ns.doc(description="Delete a chat conversation")
@console_ns.doc(params={"app_id": "Application ID", "conversation_id": "Conversation ID"})
@console_ns.response(HTTPStatus.NO_CONTENT, "Conversation deleted successfully")
@console_ns.response(HTTPStatus.UNAUTHORIZED, "Authentication required")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_ns.response(HTTPStatus.NOT_FOUND, "App or conversation not found")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_EDIT, PlainApp()),),
)
def delete(self, request_context: RequestContext, app_id: UUID, conversation_id: UUID) -> tuple[str, HTTPStatus]:
try:
application_services().conversation_commands.delete_console(
request_context, str(app_id), str(conversation_id), kind="chat"
)
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError(str(exc)) from exc
except ConversationNotExistsError as exc:
raise ConversationNotFoundHTTPError() from exc
return "", HTTPStatus.NO_CONTENT
@@ -0,0 +1,129 @@
from __future__ import annotations
from datetime import datetime
from typing import Any
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AppNotFoundError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import (
RBACPermission,
validate_request,
)
from extensions.ext_application_services import application_services
from fields._value_type_serializer import serialize_value_type
from fields.base import ResponseModel
from libs.external_api import error_responses
from libs.helper import dump_response, to_timestamp
from machinery.context import RequestContext
from services.conversation_variable_query import CONVERSATION_VARIABLE_LIMIT, ConversationVariableAppNotFoundError
class ConversationVariablesQuery(BaseModel):
conversation_id: str = Field(..., description="Conversation ID to filter variables")
class ConversationVariableResponse(ResponseModel):
id: str
name: str
value_type: str
value: str | None = None
description: str | None = None
created_at: int | None = None
updated_at: int | None = None
@field_validator("value_type", mode="before")
@classmethod
def _normalize_value_type(cls, value: Any) -> str:
exposed_type = getattr(value, "exposed_type", None)
if callable(exposed_type):
return str(exposed_type())
if isinstance(value, str):
return value
try:
return serialize_value_type(value)
except Exception:
return serialize_value_type({"value_type": value})
@field_validator("value", mode="before")
@classmethod
def _normalize_value(cls, value: Any | None) -> str | None:
if value is None:
return None
if isinstance(value, str):
return value
return str(value)
@field_validator("created_at", "updated_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class PaginatedConversationVariableResponse(ResponseModel):
page: int
limit: int
total: int
has_more: bool
data: list[ConversationVariableResponse]
register_schema_models(
console_ns,
ConversationVariablesQuery,
ConversationVariableResponse,
PaginatedConversationVariableResponse,
)
@console_ns.route("/apps/<uuid:app_id>/conversation-variables")
class ConversationVariablesApi(Resource):
@error_responses(AppNotFoundError)
@console_ns.doc("get_conversation_variables")
@console_ns.doc(description="Get conversation variables for an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(ConversationVariablesQuery))
@console_ns.response(
200,
"Conversation variables retrieved successfully",
console_ns.models[PaginatedConversationVariableResponse.__name__],
)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())],
)
def get(self, request_context: RequestContext, app_id: UUID):
queries = application_services().conversation_variables
# Preserve the legacy app-not-found/mode check before query validation.
try:
queries.require_app(request_context, str(app_id))
except ConversationVariableAppNotFoundError as error:
raise AppNotFoundError(str(error)) from error
req_data = validate_request(ConversationVariablesQuery)
rows = queries.list_variables(request_context, str(app_id), req_data.conversation_id)
return dump_response(
PaginatedConversationVariableResponse,
{
"page": 1,
"limit": CONVERSATION_VARIABLE_LIMIT,
"total": len(rows),
"has_more": False,
"data": [
ConversationVariableResponse.model_validate(
{
"created_at": row.created_at,
"updated_at": row.updated_at,
**row.variable.model_dump(),
"id": row.id,
}
)
for row in rows
],
},
)
@@ -0,0 +1,220 @@
from http import HTTPStatus
from libs.exception import BaseHTTPException
class AnnotationIdsRequiredError(BaseHTTPException):
error_code = "bad_request"
description = "annotation_ids are required if the parameter is provided."
code = HTTPStatus.BAD_REQUEST
class AnnotationImportRateLimitError(BaseHTTPException):
error_code = "too_many_requests"
code = HTTPStatus.TOO_MANY_REQUESTS
class AppNotFoundError(BaseHTTPException):
error_code = "app_not_found"
description = "App not found."
code = 404
class AppTriggerNotFoundError(BaseHTTPException):
error_code = "app_trigger_not_found"
description = "Trigger not found."
code = HTTPStatus.NOT_FOUND
class WebhookTriggerNotFoundError(BaseHTTPException):
error_code = "webhook_trigger_not_found"
description = "Webhook trigger not found for this node."
code = HTTPStatus.NOT_FOUND
class ProviderNotInitializeError(BaseHTTPException):
error_code = "provider_not_initialize"
description = (
"No valid model provider credentials found. "
"Please go to Settings -> Model Provider to complete your provider credentials."
)
code = 400
class ProviderQuotaExceededError(BaseHTTPException):
error_code = "provider_quota_exceeded"
description = (
"Your quota for Dify Hosted Model Provider has been exhausted. "
"Please go to Settings -> Model Provider to complete your own provider credentials."
)
code = 400
class ProviderModelCurrentlyNotSupportError(BaseHTTPException):
error_code = "model_currently_not_support"
description = "Dify Hosted OpenAI trial currently not support the GPT-4 model."
code = 400
class ConversationCompletedError(BaseHTTPException):
error_code = "conversation_completed"
description = "The conversation has ended. Please start a new conversation."
code = 400
class AppUnavailableError(BaseHTTPException):
error_code = "app_unavailable"
description = "App unavailable, please check your app configurations."
code = 400
class CompletionRequestError(BaseHTTPException):
error_code = "completion_request_error"
description = "Completion request failed."
code = 400
class AppMoreLikeThisDisabledError(BaseHTTPException):
error_code = "app_more_like_this_disabled"
description = "The 'More like this' feature is disabled. Please refresh your page."
code = 403
class NoAudioUploadedError(BaseHTTPException):
error_code = "no_audio_uploaded"
description = "Please upload your audio."
code = 400
class AudioTooLargeError(BaseHTTPException):
error_code = "audio_too_large"
description = "Audio size exceeded. {message}"
code = 413
class UnsupportedAudioTypeError(BaseHTTPException):
error_code = "unsupported_audio_type"
description = "Audio type not allowed."
code = 415
class ProviderNotSupportSpeechToTextError(BaseHTTPException):
error_code = "provider_not_support_speech_to_text"
description = "Provider not support speech to text."
code = 400
class ProviderNotSupportTextToSpeechError(BaseHTTPException):
error_code = "provider_not_support_text_to_speech"
description = "Provider does not support text to speech."
code = 400
class SpeechToTextDisabledError(BaseHTTPException):
error_code = "speech_to_text_disabled"
description = "Speech to text is disabled."
code = 400
class DraftWorkflowNotExist(BaseHTTPException):
error_code = "draft_workflow_not_exist"
description = "Draft workflow need to be initialized."
code = 404
class DraftWorkflowNotSync(BaseHTTPException):
error_code = "draft_workflow_not_sync"
description = "Workflow graph might have been modified, please refresh and resubmit."
code = 409
class TracingConfigNotFoundError(BaseHTTPException):
error_code = "trace_config_not_found"
description = "Tracing configuration not found."
code = 404
class TracingConfigAlreadyExistsError(BaseHTTPException):
error_code = "trace_config_already_exists"
description = "A tracing configuration already exists for this provider."
code = 409
class UnsupportedTracingProviderError(BaseHTTPException):
error_code = "unsupported_tracing_provider"
description = "The tracing provider is not supported."
code = 400
class TracingProviderUnavailableError(BaseHTTPException):
error_code = "tracing_provider_unavailable"
description = "The tracing provider is not available in this deployment."
code = 400
class InvalidTracingConfigError(BaseHTTPException):
error_code = "invalid_tracing_config"
description = "The tracing configuration is invalid."
code = 400
class TracingConfigVerificationFailedError(BaseHTTPException):
error_code = "tracing_config_verification_failed"
description = "The tracing configuration could not be verified."
code = 400
class TracingConfigProcessingError(BaseHTTPException):
error_code = "tracing_config_processing_failed"
description = "The tracing configuration could not be processed."
code = 500
class InvokeRateLimitError(BaseHTTPException):
"""Raised when the Invoke returns rate limit error."""
error_code = "rate_limit_error"
description = "Rate Limit Error"
code = 429
class NeedAddIdsError(BaseHTTPException):
error_code = "need_add_ids"
description = "Need to add ids."
code = 400
class AgentSandboxBindingNotFoundHTTPError(BaseHTTPException):
error_code = "no_active_binding"
description = "No active sandbox binding."
code = 404
class AgentSandboxUnavailableHTTPError(BaseHTTPException):
error_code = "inspector_unavailable"
description = "Sandbox inspector is unavailable."
code = 503
class AgentSandboxDownloadUnavailableHTTPError(BaseHTTPException):
error_code = "binding_file_download_unavailable"
description = "Sandbox file download is unavailable."
code = 502
class AgentBackendUnreachableError(BaseHTTPException):
error_code = "agent_backend_unreachable"
description = "Agent backend is unreachable."
code = 502
class AgentNotBoundError(BaseHTTPException):
error_code = "agent_not_bound"
description = "no agent is bound for this app/node"
code = 400
class TracingConfigConflictError(BaseHTTPException):
error_code = "tracing_config_conflict"
description = "Tracing configuration changed. Refresh and try again."
code = 409
@@ -0,0 +1,698 @@
import json
from collections.abc import Generator, Sequence
from typing import Any, Literal
from flask_restx import Resource
from pydantic import BaseModel, ConfigDict, Field, RootModel
from sqlalchemy import select
from sqlalchemy.orm import Session
from controllers.common.fields import ErrorMessageResponse, SimpleDataResponse
from controllers.common.schema import register_enum_models, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import (
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
from controllers.console.app.wraps import with_session
from controllers.console.wraps import (
account_initialization_required,
model_validate,
setup_required,
with_current_tenant_id,
)
from core.app.app_config.entities import ModelConfig
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
from core.helper.code_executor.code_node_provider import CodeNodeProvider
from core.helper.code_executor.javascript.javascript_code_provider import JavascriptCodeProvider
from core.helper.code_executor.python3.python3_code_provider import Python3CodeProvider
from core.llm_generator.entities import RuleCodeGeneratePayload, RuleGeneratePayload, RuleStructuredOutputPayload
from core.llm_generator.llm_generator import LLMGenerator
from core.workflow.generator.types import WorkflowGenerateErrorCode
from fields.base import ResponseModel
from graphon.model_runtime.entities.llm_entities import LLMMode
from graphon.model_runtime.errors.invoke import InvokeError
from libs.external_api import error_responses
from libs.helper import compact_generate_response, dump_response
from libs.login import login_required
from models import App
from services.workflow_generator_service import WorkflowGeneratorService
from services.workflow_service import WorkflowService
class InstructionGeneratePayload(BaseModel):
flow_id: str = Field(..., description="Workflow/Flow ID")
node_id: str = Field(default="", description="Node ID for workflow context")
current: str = Field(default="", description="Current instruction text")
language: str = Field(default="javascript", description="Programming language (javascript/python)")
instruction: str = Field(..., description="Instruction for generation")
model_config_data: ModelConfig = Field(
...,
alias="model_config",
description="Model configuration",
)
ideal_output: str = Field(default="", description="Expected ideal output")
class InstructionTemplatePayload(BaseModel):
type: str = Field(..., description="Instruction template type")
# Upper bound for the generator's free-text inputs. Generous for prose (a
# detailed instruction rarely passes 2k chars) while keeping the
# planner+builder prompts well inside every mainstream context window.
# Mirrored by the ``maxLength`` on the frontend generator textarea.
_MAX_INSTRUCTION_LENGTH = 10_000
class WorkflowGraphPosition(BaseModel):
x: float
y: float
class WorkflowGraphViewport(WorkflowGraphPosition):
zoom: float
class WorkflowGraphNode(BaseModel):
"""React Flow node shape accepted and returned by the generator.
Node-specific configuration lives under ``data`` and wrapper metadata
differs for container children, so unknown wrapper fields must survive
request validation and response serialization.
"""
model_config = ConfigDict(extra="allow", populate_by_name=True)
id: str
type: str
position: WorkflowGraphPosition
data: dict[str, Any]
class WorkflowGraphEdge(BaseModel):
"""React Flow edge shape with extensible renderer metadata."""
model_config = ConfigDict(extra="allow", populate_by_name=True)
id: str
source: str
target: str
type: str
class WorkflowGraph(BaseModel):
nodes: list[WorkflowGraphNode]
edges: list[WorkflowGraphEdge]
viewport: WorkflowGraphViewport
class WorkflowGeneratePayload(BaseModel):
"""Payload for the cmd+k `/create` and `/refine` workflow generator endpoint.
See ``services/workflow_generator_service.py`` for behaviour. Errors are
surfaced through the same envelope as ``/rule-generate`` so the frontend
can reuse its existing handler.
"""
mode: Literal["workflow", "advanced-chat", "auto"] = Field(
...,
description="Target app mode for the generated graph; 'auto' lets the backend classify the instruction",
)
instruction: str = Field(..., description="Natural-language workflow description")
ideal_output: str = Field(default="", description="Optional sample output for grounding")
model_config_data: ModelConfig = Field(
...,
alias="model_config",
description="Model configuration",
)
current_graph: WorkflowGraph | None = Field(
default=None,
description="Existing draft graph to refine (cmd+k `/refine`); omit for create-from-scratch",
)
class WorkflowInstructionSuggestionsPayload(BaseModel):
"""Payload for the workflow-generator instruction-suggestions endpoint.
Runs before the user picks a model, so the suggestions come from the
tenant's default model. The underlying generator never raises — an empty
``suggestions`` list is a valid 200 (soft-fail).
"""
mode: Literal["workflow", "advanced-chat"] = Field(..., description="Target app mode for the suggestions")
language: str | None = Field(default=None, description="Optional language to write the suggestions in")
count: int = Field(default=4, ge=1, le=6, description="Number of suggestions to return (1-6)")
class WorkflowGenerateErrorResponse(ResponseModel):
code: WorkflowGenerateErrorCode
detail: str
node_id: str | None = None
class WorkflowGenerateRejectedResponse(ResponseModel):
"""400 body when the boundary guard rejects the instruction before generating."""
error: str
errors: list[WorkflowGenerateErrorResponse]
class WorkflowGenerateResponse(ResponseModel):
graph: WorkflowGraph
message: str = ""
app_name: str = ""
icon: str = ""
error: str = ""
errors: list[WorkflowGenerateErrorResponse] = Field(default_factory=list)
mode: Literal["workflow", "advanced-chat"] | None = None
class WorkflowPlanNodeResponse(ResponseModel):
label: str
node_type: str
purpose: str = ""
class WorkflowPlanStartInputResponse(ResponseModel):
variable: str
label: str = ""
type: str = ""
class WorkflowGeneratePlanEventResponse(ResponseModel):
event: Literal["plan"] = "plan"
title: str = ""
description: str = ""
app_name: str = ""
icon: str = ""
mode: Literal["workflow", "advanced-chat"]
nodes: list[WorkflowPlanNodeResponse]
start_inputs: list[WorkflowPlanStartInputResponse] = Field(default_factory=list)
class WorkflowGenerateResultEventResponse(WorkflowGenerateResponse):
event: Literal["result"] = "result"
class WorkflowGenerateStreamEventResponse(
RootModel[WorkflowGeneratePlanEventResponse | WorkflowGenerateResultEventResponse]
):
"""Schema for each JSON object carried by an SSE ``data:`` frame."""
class WorkflowInstructionSuggestionsResponse(ResponseModel):
suggestions: list[str]
class GeneratorResponse(RootModel[Any]):
root: Any
class RuleGenerateResponse(ResponseModel):
prompt: str
variables: list[str]
opening_statement: str
error: str
class StructuredOutputGenerateResponse(ResponseModel):
output: str = Field(description="Generated JSON Schema, empty when generation failed")
error: str = Field(description="Failure reason, empty on success")
register_enum_models(console_ns, LLMMode)
register_schema_models(
console_ns,
RuleGeneratePayload,
RuleCodeGeneratePayload,
RuleStructuredOutputPayload,
InstructionGeneratePayload,
InstructionTemplatePayload,
WorkflowGeneratePayload,
WorkflowInstructionSuggestionsPayload,
ModelConfig,
)
register_response_schema_models(
console_ns,
GeneratorResponse,
RuleGenerateResponse,
StructuredOutputGenerateResponse,
SimpleDataResponse,
WorkflowGenerateResponse,
WorkflowGenerateRejectedResponse,
ErrorMessageResponse,
WorkflowGeneratePlanEventResponse,
WorkflowGenerateResultEventResponse,
WorkflowGenerateStreamEventResponse,
WorkflowInstructionSuggestionsResponse,
)
@console_ns.route("/rule-generate")
class RuleGenerateApi(Resource):
@error_responses(
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("generate_rule_config")
@console_ns.doc(description="Generate rule configuration using LLM")
@console_ns.expect(console_ns.models[RuleGeneratePayload.__name__])
@console_ns.response(
200,
"Rule configuration generated successfully",
console_ns.models[RuleGenerateResponse.__name__],
)
@console_ns.response(400, "Invalid request parameters")
@setup_required
@login_required
@account_initialization_required
@with_current_tenant_id
@model_validate(RuleGeneratePayload)
def post(self, req_data: RuleGeneratePayload, current_tenant_id: str):
try:
rules = LLMGenerator.generate_rule_config(tenant_id=current_tenant_id, args=req_data)
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
return dump_response(RuleGenerateResponse, rules)
@console_ns.route("/rule-code-generate")
class RuleCodeGenerateApi(Resource):
@error_responses(
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("generate_rule_code")
@console_ns.doc(description="Generate code rules using LLM")
@console_ns.expect(console_ns.models[RuleCodeGeneratePayload.__name__])
@console_ns.response(200, "Code rules generated successfully", console_ns.models[GeneratorResponse.__name__])
@console_ns.response(400, "Invalid request parameters")
@setup_required
@login_required
@account_initialization_required
@with_current_tenant_id
@model_validate(RuleCodeGeneratePayload)
def post(self, req_data: RuleCodeGeneratePayload, current_tenant_id: str):
try:
code_result = LLMGenerator.generate_code(
tenant_id=current_tenant_id,
args=req_data,
)
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
return code_result
@console_ns.route("/rule-structured-output-generate")
class RuleStructuredOutputGenerateApi(Resource):
@error_responses(
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("generate_structured_output")
@console_ns.doc(description="Generate structured output rules using LLM")
@console_ns.expect(console_ns.models[RuleStructuredOutputPayload.__name__])
@console_ns.response(
200,
"Structured output generated successfully",
console_ns.models[StructuredOutputGenerateResponse.__name__],
)
@console_ns.response(400, "Invalid request parameters")
@setup_required
@login_required
@account_initialization_required
@with_current_tenant_id
@model_validate(RuleStructuredOutputPayload)
def post(self, req_data: RuleStructuredOutputPayload, current_tenant_id: str):
try:
structured_output = LLMGenerator.generate_structured_output(
tenant_id=current_tenant_id,
args=req_data,
)
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
return structured_output
@console_ns.route("/instruction-generate")
class InstructionGenerateApi(Resource):
@error_responses(
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("generate_instruction")
@console_ns.doc(description="Generate instruction for workflow nodes or general use")
@console_ns.expect(console_ns.models[InstructionGeneratePayload.__name__])
@console_ns.response(200, "Instruction generated successfully", console_ns.models[GeneratorResponse.__name__])
@console_ns.response(
400,
"Invalid request parameters or flow/workflow not found",
console_ns.models[ErrorMessageResponse.__name__],
)
@setup_required
@login_required
@account_initialization_required
@with_current_tenant_id
@with_session(write=False)
@model_validate(InstructionGeneratePayload)
def post(self, req_data: InstructionGeneratePayload, session: Session, current_tenant_id: str):
providers: list[type[CodeNodeProvider]] = [Python3CodeProvider, JavascriptCodeProvider]
code_provider: type[CodeNodeProvider] | None = next(
(p for p in providers if p.is_accept_language(req_data.language)), None
)
code_template = code_provider.get_default_code() if code_provider else ""
try:
# Generate from nothing for a workflow node
if (req_data.current in (code_template, "")) and req_data.node_id != "":
app = session.scalar(
select(App).where(App.id == req_data.flow_id, App.tenant_id == current_tenant_id).limit(1)
)
if not app:
return {"error": f"app {req_data.flow_id} not found"}, 400
workflow = WorkflowService().get_draft_workflow(app_model=app, session=session)
if not workflow:
return {"error": f"workflow {req_data.flow_id} not found"}, 400
nodes: Sequence = workflow.graph_dict["nodes"]
node = [node for node in nodes if node["id"] == req_data.node_id]
if len(node) == 0:
return {"error": f"node {req_data.node_id} not found"}, 400
node_type = node[0]["data"]["type"]
match node_type:
case "llm":
return LLMGenerator.generate_rule_config(
current_tenant_id,
app_id=req_data.flow_id,
args=RuleGeneratePayload(
instruction=req_data.instruction,
model_config=req_data.model_config_data,
no_variable=True,
),
)
case "agent":
return LLMGenerator.generate_rule_config(
current_tenant_id,
app_id=req_data.flow_id,
args=RuleGeneratePayload(
instruction=req_data.instruction,
model_config=req_data.model_config_data,
no_variable=True,
),
)
case "code":
return LLMGenerator.generate_code(
tenant_id=current_tenant_id,
app_id=req_data.flow_id,
args=RuleCodeGeneratePayload(
instruction=req_data.instruction,
model_config=req_data.model_config_data,
code_language=req_data.language,
),
)
case _:
return {"error": f"invalid node type: {node_type}"}
if req_data.node_id == "" and req_data.current != "": # For legacy app without a workflow
return LLMGenerator.instruction_modify_legacy(
tenant_id=current_tenant_id,
flow_id=req_data.flow_id,
current=req_data.current,
instruction=req_data.instruction,
model_config=req_data.model_config_data,
ideal_output=req_data.ideal_output,
session=session,
)
if req_data.node_id != "" and req_data.current != "": # For workflow node
return LLMGenerator.instruction_modify_workflow(
tenant_id=current_tenant_id,
flow_id=req_data.flow_id,
node_id=req_data.node_id,
current=req_data.current,
instruction=req_data.instruction,
model_config=req_data.model_config_data,
ideal_output=req_data.ideal_output,
workflow_service=WorkflowService(),
session=session,
)
return {"error": "incompatible parameters"}, 400
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
@console_ns.route("/instruction-generate/template")
class InstructionGenerationTemplateApi(Resource):
@console_ns.doc("get_instruction_template")
@console_ns.doc(description="Get instruction generation template")
@console_ns.expect(console_ns.models[InstructionTemplatePayload.__name__])
@console_ns.response(200, "Template retrieved successfully", console_ns.models[SimpleDataResponse.__name__])
@console_ns.response(400, "Invalid request parameters")
@setup_required
@login_required
@account_initialization_required
@model_validate(InstructionTemplatePayload)
def post(self, req_data: InstructionTemplatePayload):
match req_data.type:
case "prompt":
from core.llm_generator.prompts import INSTRUCTION_GENERATE_TEMPLATE_PROMPT
return {"data": INSTRUCTION_GENERATE_TEMPLATE_PROMPT}
case "code":
from core.llm_generator.prompts import INSTRUCTION_GENERATE_TEMPLATE_CODE
return {"data": INSTRUCTION_GENERATE_TEMPLATE_CODE}
case _:
raise ValueError(f"Invalid type: {req_data.type}")
def _workflow_instruction_guard(args: WorkflowGeneratePayload) -> tuple[dict, int] | None:
"""Shared boundary guard for the workflow-generate endpoints.
Returns a ``(body, 400)`` tuple when the instruction is empty / whitespace
or either free-text field exceeds the cap, else ``None``. Pydantic only
validates the field is a str; a whitespace-only or pasted-document input
would otherwise waste a slow planner+builder roundtrip on a response the
validator rejects anyway. Both the blocking and streaming endpoints call
this so they reject identical inputs.
"""
if not args.instruction.strip():
return {
"error": "Instruction is required",
"errors": [{"code": WorkflowGenerateErrorCode.EMPTY_INSTRUCTION, "detail": "Instruction is required"}],
}, 400
if len(args.instruction) > _MAX_INSTRUCTION_LENGTH or len(args.ideal_output) > _MAX_INSTRUCTION_LENGTH:
return {
"error": "Instruction is too long",
"errors": [
{
"code": WorkflowGenerateErrorCode.INSTRUCTION_TOO_LONG,
"detail": f"Instruction and ideal output must each be at most {_MAX_INSTRUCTION_LENGTH} characters",
}
],
}, 400
return None
@console_ns.route("/workflow-generate")
class WorkflowGenerateApi(Resource):
"""Generate a Workflow / Chatflow draft graph from a natural-language description.
Triggered by the cmd+k `/create` slash command. Returns a graph payload
shaped exactly like ``WorkflowService.sync_draft_workflow``'s input, so the
frontend can hand it straight to ``/apps/{id}/workflows/draft``.
"""
@error_responses(
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("generate_workflow_graph")
@console_ns.doc(description="Generate a Dify workflow graph from natural language")
@console_ns.expect(console_ns.models[WorkflowGeneratePayload.__name__])
@console_ns.response(
200,
"Workflow graph generated successfully",
console_ns.models[WorkflowGenerateResponse.__name__],
)
@console_ns.response(
400, "Invalid request parameters", console_ns.models[WorkflowGenerateRejectedResponse.__name__]
)
@setup_required
@login_required
@account_initialization_required
@with_current_tenant_id
@model_validate(WorkflowGeneratePayload)
def post(self, req_data: WorkflowGeneratePayload, current_tenant_id: str):
# Reject empty / over-length instructions at the boundary (shared with
# the streaming endpoint) before spending a planner+builder roundtrip.
guard = _workflow_instruction_guard(req_data)
if guard is not None:
return guard
try:
result = WorkflowGeneratorService.generate_workflow_graph(
tenant_id=current_tenant_id,
mode=req_data.mode,
instruction=req_data.instruction,
model_config=req_data.model_config_data,
ideal_output=req_data.ideal_output,
current_graph=req_data.current_graph.model_dump(by_alias=True, exclude_none=True)
if req_data.current_graph
else None,
)
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
return dump_response(WorkflowGenerateResponse, result)
@console_ns.route("/workflow-generate/suggestions")
class WorkflowInstructionSuggestionsApi(Resource):
"""Suggest short, buildable example instructions for the cmd+k generator.
Runs before a model is selected (uses the tenant's default model). The
underlying generator never raises, so an empty list is a valid 200 — the
frontend renders "no suggestions" rather than an error, so no provider-error
mapping is needed here.
"""
@console_ns.doc("generate_workflow_instruction_suggestions")
@console_ns.doc(description="Suggest example workflow-generator instructions for the tenant")
@console_ns.expect(console_ns.models[WorkflowInstructionSuggestionsPayload.__name__])
@console_ns.response(
200,
"Suggestions generated successfully",
console_ns.models[WorkflowInstructionSuggestionsResponse.__name__],
)
@console_ns.response(400, "Invalid request parameters")
@setup_required
@login_required
@account_initialization_required
@with_current_tenant_id
@model_validate(WorkflowInstructionSuggestionsPayload)
def post(self, req_data: WorkflowInstructionSuggestionsPayload, current_tenant_id: str):
suggestions = LLMGenerator.generate_workflow_instruction_suggestions(
tenant_id=current_tenant_id,
mode=req_data.mode,
language=req_data.language,
count=req_data.count,
)
return dump_response(WorkflowInstructionSuggestionsResponse, {"suggestions": suggestions})
@console_ns.route("/workflow-generate/stream")
class WorkflowGenerateStreamApi(Resource):
"""Plan-first streaming variant of ``/workflow-generate`` (Server-Sent Events).
Emits a ``plan`` event (high-level node list + app metadata) as soon as the
planner returns, then a final ``result`` event with the full graph — the
SAME envelope ``/workflow-generate`` returns. Provider-init / invoke errors
are surfaced as a single ``result`` event (code ``MODEL_ERROR``) so the
frontend's stream parser always receives a result rather than a non-SSE HTTP
error.
"""
@console_ns.doc("generate_workflow_graph_stream")
@console_ns.doc(description="Stream a Dify workflow graph (plan then result) via SSE")
@console_ns.expect(console_ns.models[WorkflowGeneratePayload.__name__])
@console_ns.response(
200,
"Server-Sent Events stream; each data frame matches this plan/result event schema",
console_ns.models[WorkflowGenerateStreamEventResponse.__name__],
)
@console_ns.response(
400, "Invalid request parameters", console_ns.models[WorkflowGenerateRejectedResponse.__name__]
)
@setup_required
@login_required
@account_initialization_required
@with_current_tenant_id
@model_validate(WorkflowGeneratePayload)
def post(self, req_data: WorkflowGeneratePayload, current_tenant_id: str):
# Same boundary guards as the blocking endpoint — return a normal 400
# JSON for these BEFORE opening the stream.
guard = _workflow_instruction_guard(req_data)
if guard is not None:
return guard
def generate() -> Generator[str]:
try:
for event_name, payload in WorkflowGeneratorService.generate_workflow_graph_stream(
tenant_id=current_tenant_id,
mode=req_data.mode,
instruction=req_data.instruction,
model_config=req_data.model_config_data,
ideal_output=req_data.ideal_output,
current_graph=(
req_data.current_graph.model_dump(by_alias=True, exclude_none=True)
if req_data.current_graph
else None
),
):
body = {"event": event_name, **payload}
if event_name == "plan":
plan_event = WorkflowGeneratePlanEventResponse.model_validate(body)
yield f"data: {json.dumps(plan_event.model_dump(mode='json'))}\n\n"
else:
result_event = WorkflowGenerateResultEventResponse.model_validate(body)
yield f"data: {json.dumps(result_event.model_dump(mode='json'))}\n\n"
except (ProviderTokenNotInitError, QuotaExceededError, ModelCurrentlyNotSupportError, InvokeError) as e:
# The model instance is resolved inside the service (lazily, on
# first iteration), so a provider / init error surfaces here.
# Emit it as a single SSE result event rather than a non-SSE
# error response so the frontend's stream parser always gets a
# result it can render.
detail = getattr(e, "description", None) or str(e) or "Model invocation failed"
error_body = {
"event": "result",
"graph": {"nodes": [], "edges": [], "viewport": {"x": 0.0, "y": 0.0, "zoom": 0.7}},
"error": detail,
"errors": [{"code": WorkflowGenerateErrorCode.MODEL_ERROR, "detail": detail}],
}
error_event = WorkflowGenerateResultEventResponse.model_validate(error_body)
yield f"data: {json.dumps(error_event.model_dump(mode='json'))}\n\n"
return compact_generate_response(generate())
@@ -0,0 +1,167 @@
import json
from datetime import datetime
from typing import Any
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from werkzeug.exceptions import Conflict, NotFound
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AppNotFoundError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import RBACPermission, validate_request
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from libs.external_api import error_responses
from libs.helper import dump_response, to_timestamp
from machinery.context import RequestContext
from models.account import TenantAccountRole
from services.app.mcp_server_service import (
AppMCPServerAlreadyExistsError,
AppMCPServerAppNotFoundError,
AppMCPServerNotFoundError,
AppMCPServerStatus,
)
_EDIT_ROLES = frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR})
class MCPServerCreatePayload(BaseModel):
description: str | None = Field(default=None, description="Server description")
parameters: dict[str, Any] = Field(
...,
description="Server parameters configuration",
)
class MCPServerUpdatePayload(BaseModel):
id: UUID = Field(..., description="Server ID")
description: str | None = Field(default=None, description="Server description")
parameters: dict[str, Any] = Field(
...,
description="Server parameters configuration",
)
status: AppMCPServerStatus | None = Field(default=None, description="Server status")
class AppMCPServerResponse(ResponseModel):
id: str
name: str
server_code: str
description: str
status: AppMCPServerStatus
parameters: dict[str, Any] | list[Any] | str
created_at: int | None = None
updated_at: int | None = None
@field_validator("parameters", mode="before")
@classmethod
def _normalize_parameters(cls, value: Any) -> Any:
if isinstance(value, str):
try:
return json.loads(value)
except (json.JSONDecodeError, TypeError):
return value
return value
@field_validator("created_at", "updated_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
register_schema_models(console_ns, MCPServerCreatePayload, MCPServerUpdatePayload, AppMCPServerResponse)
@console_ns.route("/apps/<uuid:app_id>/server")
class AppMCPServerController(Resource):
@error_responses(AppNotFoundError)
@console_ns.doc("get_app_mcp_server")
@console_ns.doc(description="Get MCP server configuration for an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
200, "MCP server configuration retrieved successfully", console_ns.models[AppMCPServerResponse.__name__]
)
@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp())])
def get(self, request_context: RequestContext, app_id: UUID):
try:
server = application_services().app_mcp_servers.get(request_context, str(app_id))
except AppMCPServerAppNotFoundError as error:
raise AppNotFoundError() from error
if server is None:
return {}
return dump_response(AppMCPServerResponse, server)
@error_responses(AppNotFoundError)
@console_ns.doc("create_app_mcp_server")
@console_ns.doc(description="Create MCP server configuration for an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[MCPServerCreatePayload.__name__])
@console_ns.response(
201, "MCP server configuration created successfully", console_ns.models[AppMCPServerResponse.__name__]
)
@console_ns.response(403, "Insufficient permissions")
@console_ns.response(409, "MCP server already exists for this app")
@console_account_admission(allowed_roles=_EDIT_ROLES, rbac_checks=[RBACCheck(RBACPermission.APP_EDIT, PlainApp())])
def post(self, request_context: RequestContext, app_id: UUID):
payload = validate_request(MCPServerCreatePayload)
try:
server = application_services().app_mcp_servers.create(
request_context, str(app_id), description=payload.description, parameters=payload.parameters
)
except AppMCPServerAppNotFoundError as error:
raise AppNotFoundError() from error
except AppMCPServerAlreadyExistsError as error:
raise Conflict(str(error)) from error
return dump_response(AppMCPServerResponse, server), 201
@error_responses(AppNotFoundError)
@console_ns.doc("update_app_mcp_server")
@console_ns.doc(description="Update MCP server configuration for an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[MCPServerUpdatePayload.__name__])
@console_ns.response(
200, "MCP server configuration updated successfully", console_ns.models[AppMCPServerResponse.__name__]
)
@console_ns.response(403, "Insufficient permissions")
@console_ns.response(404, "Server not found")
@console_account_admission(allowed_roles=_EDIT_ROLES, rbac_checks=[RBACCheck(RBACPermission.APP_EDIT, PlainApp())])
def put(self, request_context: RequestContext, app_id: UUID):
payload = validate_request(MCPServerUpdatePayload)
try:
server = application_services().app_mcp_servers.update(
request_context,
str(app_id),
server_id=str(payload.id),
description=payload.description,
parameters=payload.parameters,
status=payload.status,
)
except AppMCPServerAppNotFoundError as error:
raise AppNotFoundError() from error
except AppMCPServerNotFoundError as error:
raise NotFound from error
return dump_response(AppMCPServerResponse, server)
@console_ns.route("/apps/<uuid:app_id>/server/refresh")
class AppMCPServerRefreshController(Resource):
@error_responses(AppNotFoundError)
@console_ns.doc("refresh_app_mcp_server")
@console_ns.doc(description="Refresh MCP server configuration and regenerate server code")
@console_ns.doc(params={"app_id": "App ID"})
@console_ns.response(200, "MCP server refreshed successfully", console_ns.models[AppMCPServerResponse.__name__])
@console_ns.response(403, "Insufficient permissions")
@console_ns.response(404, "Server not found")
@console_account_admission(allowed_roles=_EDIT_ROLES, rbac_checks=[RBACCheck(RBACPermission.APP_EDIT, PlainApp())])
def post(self, request_context: RequestContext, app_id: UUID):
try:
server = application_services().app_mcp_servers.refresh(request_context, str(app_id))
except AppMCPServerAppNotFoundError as error:
raise AppNotFoundError() from error
except AppMCPServerNotFoundError as error:
raise NotFound from error
return dump_response(AppMCPServerResponse, server)
@@ -0,0 +1,413 @@
import logging
from http import HTTPStatus
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from controllers.common.controller_schemas import MessageFeedbackPayload as _MessageFeedbackPayloadBase
from controllers.common.errors import InternalServerError as InternalServerHTTPError
from controllers.common.errors import MessageFeedbackRatingRequiredError, NotFoundError, UnauthorizedError
from controllers.common.fields import SimpleResultResponse
from controllers.common.rbac import AgentId, PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import (
AppNotFoundError,
AppUnavailableError,
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
from controllers.console.explore.error import AppSuggestedQuestionsAfterAnswerDisabledError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import (
RBACPermission,
model_validate,
)
from core.entities.execution_extra_content import ExecutionExtraContentDomainModel
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
from enums.account import TenantAccountRole
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from fields.conversation_fields import (
MessageDetail as BaseMessageDetailResponse,
)
from fields.message_fields import SuggestedQuestionsResponse
from graphon.model_runtime.errors.invoke import InvokeError
from libs.external_api import error_responses
from libs.helper import dump_response, uuid_value
from machinery.context import RequestContext
from models.enums import FeedbackRating
from models.model import AppMode
from services.agent.errors import AgentNotFoundError, AgentVersionNotFoundError
from services.app.agent_app_contracts import AgentAppNotFoundError
from services.app.console_service import ConsoleAppNotFoundError
from services.app_definition_query_service import AppDefinitionUnavailableError
from services.entities.message_entities import MessageAccount
from services.errors.conversation import ConversationNotExistsError
from services.errors.message import (
FeedbackRatingRequiredError,
FirstMessageNotExistsError,
MessageActorNotFoundError,
MessageNotExistsError,
SuggestedQuestionsAfterAnswerDisabledError,
)
logger = logging.getLogger(__name__)
class ChatMessagesQuery(BaseModel):
conversation_id: str = Field(..., description="Conversation ID")
first_id: str | None = Field(default=None, description="First message ID for pagination")
limit: int = Field(default=20, ge=1, le=100, description="Number of messages to return (1-100)")
@field_validator("first_id", mode="before")
@classmethod
def empty_to_none(cls, value: str | None) -> str | None:
if value == "":
return None
return value
@field_validator("conversation_id", "first_id")
@classmethod
def validate_uuid(cls, value: str | None) -> str | None:
if value is None:
return value
return uuid_value(value)
class AppMessageFeedbackPayload(_MessageFeedbackPayloadBase):
message_id: str = Field(..., description="Message ID")
@field_validator("message_id")
@classmethod
def validate_message_id(cls, value: str) -> str:
return uuid_value(value)
class MessageDetailResponse(BaseMessageDetailResponse):
extra_contents: list[ExecutionExtraContentDomainModel] = Field(default_factory=list)
class MessageInfiniteScrollPaginationResponse(ResponseModel):
limit: int
has_more: bool
data: list[MessageDetailResponse]
register_schema_models(
console_ns,
ChatMessagesQuery,
AppMessageFeedbackPayload,
)
register_response_schema_models(
console_ns,
SuggestedQuestionsResponse,
MessageDetailResponse,
MessageInfiniteScrollPaginationResponse,
SimpleResultResponse,
)
@console_ns.route("/apps/<uuid:app_id>/chat-messages")
class ChatMessageListApi(Resource):
@error_responses(AppNotFoundError, AppUnavailableError)
@console_ns.doc("list_chat_messages")
@console_ns.doc(description="Get chat messages for a conversation with pagination")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(ChatMessagesQuery)})
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[MessageInfiniteScrollPaginationResponse.__name__])
@console_ns.response(HTTPStatus.NOT_FOUND, "Conversation not found")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),),
)
@model_validate(ChatMessagesQuery)
def get(self, req_data: ChatMessagesQuery, context: RequestContext, app_id: UUID) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(context, str(app_id))
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError() from exc
if app.mode not in (AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT):
raise AppNotFoundError("App mode does not support chat messages")
return _list_chat_messages(args=req_data, context=context, app_id=app.id)
@console_ns.route("/agent/<uuid:agent_id>/chat-messages")
class AgentChatMessageListApi(Resource):
@error_responses(AgentNotFoundError, AppUnavailableError)
@console_ns.doc("list_agent_chat_messages")
@console_ns.doc(description="Get Agent App chat messages for a conversation with pagination")
@console_ns.doc(params={"agent_id": "Agent ID"})
@console_ns.doc(params=query_params_from_model(ChatMessagesQuery))
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[MessageInfiniteScrollPaginationResponse.__name__])
@console_ns.response(HTTPStatus.NOT_FOUND, "Agent or conversation not found")
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=(RBACCheck(RBACPermission.AGENT_TEST_AND_RUN, AgentId()),),
)
@model_validate(ChatMessagesQuery)
def get(self, req_data: ChatMessagesQuery, context: RequestContext, agent_id: UUID) -> dict[str, object]:
try:
app_id = application_services().agent_apps.access.resolve_existing_runtime_app_id(context, str(agent_id))
except AgentAppNotFoundError as exc:
raise AgentNotFoundError() from exc
return _list_chat_messages(args=req_data, context=context, app_id=app_id)
@console_ns.route("/apps/<uuid:app_id>/feedbacks")
class MessageFeedbackApi(Resource):
@error_responses(AppNotFoundError, AppUnavailableError, MessageFeedbackRatingRequiredError)
@console_ns.doc("create_message_feedback")
@console_ns.doc(description="Create or update message feedback (like/dislike)")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[AppMessageFeedbackPayload.__name__])
@console_ns.response(
HTTPStatus.OK, "Feedback updated successfully", console_ns.models[SimpleResultResponse.__name__]
)
@console_ns.response(HTTPStatus.NOT_FOUND, "Message not found")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions")
@console_account_admission()
@model_validate(AppMessageFeedbackPayload)
def post(self, req_data: AppMessageFeedbackPayload, context: RequestContext, app_id: UUID) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(context, str(app_id))
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError() from exc
return _update_message_feedback(args=req_data, context=context, app_id=app.id)
@console_ns.route("/agent/<uuid:agent_id>/feedbacks")
class AgentMessageFeedbackApi(Resource):
@error_responses(AgentNotFoundError, AppUnavailableError, MessageFeedbackRatingRequiredError)
@console_ns.doc("create_agent_message_feedback")
@console_ns.doc(description="Create or update Agent App message feedback")
@console_ns.doc(params={"agent_id": "Agent ID"})
@console_ns.expect(console_ns.models[AppMessageFeedbackPayload.__name__])
@console_ns.response(
HTTPStatus.OK, "Feedback updated successfully", console_ns.models[SimpleResultResponse.__name__]
)
@console_ns.response(HTTPStatus.NOT_FOUND, "Agent or message not found")
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.AGENT_TEST_AND_RUN, AgentId()),))
@model_validate(AppMessageFeedbackPayload)
def post(self, req_data: AppMessageFeedbackPayload, context: RequestContext, agent_id: UUID) -> dict[str, object]:
try:
app_id = application_services().agent_apps.access.resolve_existing_runtime_app_id(context, str(agent_id))
except AgentAppNotFoundError as exc:
raise AgentNotFoundError() from exc
return _update_message_feedback(args=req_data, context=context, app_id=app_id)
@console_ns.route("/apps/<uuid:app_id>/chat-messages/<uuid:message_id>/suggested-questions")
class MessageSuggestedQuestionApi(Resource):
@error_responses(
AgentVersionNotFoundError,
AppNotFoundError,
AppSuggestedQuestionsAfterAnswerDisabledError,
AppUnavailableError,
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("get_message_suggested_questions")
@console_ns.doc(description="Get suggested questions for a message")
@console_ns.doc(params={"app_id": "Application ID", "message_id": "Message ID"})
@console_ns.response(
HTTPStatus.OK,
"Suggested questions retrieved successfully",
console_ns.models[SuggestedQuestionsResponse.__name__],
)
@console_ns.response(HTTPStatus.BAD_REQUEST, "App or model provider unavailable, or generation failed")
@console_ns.response(HTTPStatus.UNAUTHORIZED, "Account authentication required")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions or suggested questions disabled")
@console_ns.response(HTTPStatus.NOT_FOUND, "App, message, or conversation not found")
@console_ns.response(HTTPStatus.INTERNAL_SERVER_ERROR, "Unexpected server error")
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),))
def get(self, context: RequestContext, app_id: UUID, message_id: UUID) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(context, str(app_id))
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError() from exc
if app.mode not in (AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT):
raise AppNotFoundError("App mode does not support suggested questions")
return _get_message_suggested_questions(
context=context, app_id=app.id, app_mode=app.mode, message_id=message_id
)
@console_ns.route("/agent/<uuid:agent_id>/chat-messages/<uuid:message_id>/suggested-questions")
class AgentMessageSuggestedQuestionApi(Resource):
@error_responses(
AgentNotFoundError,
AgentVersionNotFoundError,
AppSuggestedQuestionsAfterAnswerDisabledError,
AppUnavailableError,
CompletionRequestError,
ProviderModelCurrentlyNotSupportError,
ProviderNotInitializeError,
ProviderQuotaExceededError,
)
@console_ns.doc("get_agent_message_suggested_questions")
@console_ns.doc(description="Get suggested questions for an Agent App message")
@console_ns.doc(params={"agent_id": "Agent ID", "message_id": "Message ID"})
@console_ns.response(
HTTPStatus.OK,
"Suggested questions retrieved successfully",
console_ns.models[SuggestedQuestionsResponse.__name__],
)
@console_ns.response(HTTPStatus.BAD_REQUEST, "App or model provider unavailable, or generation failed")
@console_ns.response(HTTPStatus.UNAUTHORIZED, "Account authentication required")
@console_ns.response(HTTPStatus.FORBIDDEN, "Insufficient permissions or suggested questions disabled")
@console_ns.response(HTTPStatus.NOT_FOUND, "Agent, message, or conversation not found")
@console_ns.response(HTTPStatus.INTERNAL_SERVER_ERROR, "Unexpected server error")
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.AGENT_TEST_AND_RUN, AgentId()),))
def get(self, context: RequestContext, agent_id: UUID, message_id: UUID) -> dict[str, object]:
try:
app_id = application_services().agent_apps.access.resolve_existing_runtime_app_id(context, str(agent_id))
except AgentAppNotFoundError as exc:
raise AgentNotFoundError() from exc
return _get_message_suggested_questions(
context=context, app_id=app_id, app_mode=AppMode.AGENT, message_id=message_id
)
@console_ns.route("/apps/<uuid:app_id>/messages/<uuid:message_id>")
class MessageApi(Resource):
@error_responses(AppNotFoundError, AppUnavailableError)
@console_ns.doc("get_message")
@console_ns.doc(description="Get message details by ID")
@console_ns.doc(params={"app_id": "Application ID", "message_id": "Message ID"})
@console_ns.response(
HTTPStatus.OK, "Message retrieved successfully", console_ns.models[MessageDetailResponse.__name__]
)
@console_ns.response(HTTPStatus.NOT_FOUND, "Message not found")
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()),))
def get(self, context: RequestContext, app_id: UUID, message_id: UUID) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(context, str(app_id))
except ConsoleAppNotFoundError as exc:
raise AppNotFoundError() from exc
return _get_message_detail(context=context, app_id=app.id, message_id=message_id)
@console_ns.route("/agent/<uuid:agent_id>/messages/<uuid:message_id>")
class AgentMessageApi(Resource):
@error_responses(AgentNotFoundError, AppUnavailableError)
@console_ns.doc("get_agent_message")
@console_ns.doc(description="Get Agent App message details by ID")
@console_ns.doc(params={"agent_id": "Agent ID", "message_id": "Message ID"})
@console_ns.response(
HTTPStatus.OK, "Message retrieved successfully", console_ns.models[MessageDetailResponse.__name__]
)
@console_ns.response(HTTPStatus.NOT_FOUND, "Agent or message not found")
@console_account_admission(rbac_checks=(RBACCheck(RBACPermission.AGENT_TEST_AND_RUN, AgentId()),))
def get(self, context: RequestContext, agent_id: UUID, message_id: UUID) -> dict[str, object]:
try:
app_id = application_services().agent_apps.access.resolve_existing_runtime_app_id(context, str(agent_id))
except AgentAppNotFoundError as exc:
raise AgentNotFoundError() from exc
return _get_message_detail(context=context, app_id=app_id, message_id=message_id)
def _list_chat_messages(*, args: ChatMessagesQuery, context: RequestContext, app_id: str) -> dict[str, object]:
try:
page = application_services().message_queries.get_console_page(
app_id=app_id,
app_owner_tenant_id=context.active_workspace_id,
account_id=context.account_id,
conversation_id=args.conversation_id,
first_id=args.first_id,
limit=args.limit,
)
except AppDefinitionUnavailableError as exc:
raise AppUnavailableError() from exc
except MessageActorNotFoundError as exc:
raise UnauthorizedError("Account no longer exists") from exc
except ConversationNotExistsError as exc:
raise NotFoundError("Conversation Not Exists.") from exc
except FirstMessageNotExistsError as exc:
raise NotFoundError("First message not found") from exc
return dump_response(MessageInfiniteScrollPaginationResponse, page)
def _update_message_feedback(
*, args: AppMessageFeedbackPayload, context: RequestContext, app_id: str
) -> dict[str, object]:
try:
application_services().message_feedbacks.set_admin_feedback(
app_id=app_id,
app_owner_tenant_id=context.active_workspace_id,
account_id=context.account_id,
message_id=args.message_id,
rating=FeedbackRating(args.rating) if args.rating is not None else None,
content=args.content,
)
except AppDefinitionUnavailableError as exc:
raise AppUnavailableError() from exc
except MessageActorNotFoundError as exc:
raise UnauthorizedError("Account no longer exists") from exc
except MessageNotExistsError as exc:
raise NotFoundError("Message Not Exists.") from exc
except FeedbackRatingRequiredError as exc:
raise MessageFeedbackRatingRequiredError() from exc
return dump_response(SimpleResultResponse, {"result": "success"})
def _get_message_suggested_questions(
*, context: RequestContext, app_id: str, app_mode: str, message_id: UUID
) -> dict[str, object]:
try:
questions = application_services().message_suggested_questions.get_suggested_questions(
app_id=app_id,
app_owner_tenant_id=context.active_workspace_id,
expected_app_mode=app_mode,
actor=MessageAccount(account_id=context.account_id),
invoke_from="debugger",
message_id=str(message_id),
)
except AppDefinitionUnavailableError as exc:
raise AppUnavailableError() from exc
except MessageActorNotFoundError as exc:
raise UnauthorizedError("Account no longer exists") from exc
except MessageNotExistsError as caught_error:
raise NotFoundError("Message not found") from caught_error
except ConversationNotExistsError as caught_error:
raise NotFoundError("Conversation not found") from caught_error
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) from ex
except QuotaExceededError as caught_error:
raise ProviderQuotaExceededError() from caught_error
except ModelCurrentlyNotSupportError as caught_error:
raise ProviderModelCurrentlyNotSupportError() from caught_error
except InvokeError as e:
raise CompletionRequestError(e.description) from e
except SuggestedQuestionsAfterAnswerDisabledError as caught_error:
raise AppSuggestedQuestionsAfterAnswerDisabledError() from caught_error
except AgentVersionNotFoundError:
# The legacy Agent config reader still owns this HTTP error.
# Remove this compatibility case when it exposes a domain error.
raise
except Exception as caught_error:
logger.exception("internal server error.")
raise InternalServerHTTPError() from caught_error
return dump_response(SuggestedQuestionsResponse, {"data": questions})
def _get_message_detail(*, context: RequestContext, app_id: str, message_id: UUID) -> dict[str, object]:
try:
message = application_services().message_queries.get_console_message(
app_id=app_id,
app_owner_tenant_id=context.active_workspace_id,
account_id=context.account_id,
message_id=str(message_id),
)
except AppDefinitionUnavailableError as exc:
raise AppUnavailableError() from exc
except MessageActorNotFoundError as exc:
raise UnauthorizedError("Account no longer exists") from exc
except MessageNotExistsError as exc:
raise NotFoundError("Message Not Exists.") from exc
return dump_response(MessageDetailResponse, message)
@@ -0,0 +1,181 @@
import json
from flask_restx import Resource
from sqlalchemy.orm import Session
from controllers.common.fields import SimpleResultResponse
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import register_response_schema_models, register_schema_models
from controllers.common.session import with_session
from controllers.console import console_ns
from controllers.console.app.model_config_payload import AppModelConfigPayload
from controllers.console.app.wraps import get_app_model
from controllers.console.wraps import (
RBACPermission,
account_initialization_required,
edit_permission_required,
rbac_permission_required,
setup_required,
with_current_tenant_id,
with_current_user_id,
)
from core.agent.entities import AgentToolEntity
from core.tools.tool_manager import ToolManager
from core.tools.utils.configuration import ToolParameterConfigurationManager
from events.app_event import app_model_config_was_updated
from libs.datetime_utils import naive_utc_now
from libs.login import login_required
from models.model import App, AppMode, AppModelConfig
from services.app_model_config_service import AppModelConfigService
register_schema_models(console_ns, AppModelConfigPayload)
register_response_schema_models(console_ns, SimpleResultResponse)
@console_ns.route("/apps/<uuid:app_id>/model-config")
class ModelConfigResource(Resource):
@console_ns.doc("update_app_model_config")
@console_ns.doc(description="Update application model configuration")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[AppModelConfigPayload.__name__])
@console_ns.response(
200,
"Model configuration updated successfully",
console_ns.models[SimpleResultResponse.__name__],
)
@console_ns.response(400, "Invalid configuration")
@console_ns.response(404, "App not found")
@setup_required
@login_required
@edit_permission_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@account_initialization_required
@with_current_user_id
@with_current_tenant_id
@with_session
@get_app_model(mode=[AppMode.AGENT_CHAT, AppMode.CHAT, AppMode.COMPLETION])
def post(self, session: Session, current_tenant_id: str, current_user_id: str, app_model: App):
"""Modify the app model config and dataset joins in one request transaction."""
payload = AppModelConfigPayload.model_validate(console_ns.payload or {})
model_configuration = AppModelConfigService.validate_configuration(
tenant_id=current_tenant_id,
# Preserve omission so feature validators retain their existing defaults.
config=payload.model_dump(exclude_unset=True),
app_mode=AppMode.value_of(app_model.mode),
session=session,
)
new_app_model_config = AppModelConfig(
app_id=app_model.id,
created_by=current_user_id,
updated_by=current_user_id,
)
new_app_model_config = new_app_model_config.from_model_config_dict(model_configuration)
if app_model.mode == AppMode.AGENT_CHAT or app_model.is_agent_with_session(session=session):
original_app_model_config = app_model.app_model_config_with_session(session=session)
if original_app_model_config is None:
raise ValueError("Original app model config not found")
agent_mode = original_app_model_config.agent_mode_dict
# decrypt agent tool parameters if it's secret-input
parameter_map = {}
masked_parameter_map = {}
tool_map = {}
for tool in agent_mode.get("tools") or []:
if not isinstance(tool, dict) or len(tool.keys()) <= 3:
continue
agent_tool_entity = AgentToolEntity.model_validate(tool)
# get tool
try:
tool_runtime = ToolManager.get_agent_tool_runtime(
tenant_id=current_tenant_id,
app_id=app_model.id,
agent_tool=agent_tool_entity,
user_id=current_user_id,
)
manager = ToolParameterConfigurationManager(
tenant_id=current_tenant_id,
tool_runtime=tool_runtime,
provider_name=agent_tool_entity.provider_id,
provider_type=agent_tool_entity.provider_type,
identity_id=f"AGENT.{app_model.id}",
)
except Exception:
continue
# get decrypted parameters
if agent_tool_entity.tool_parameters:
parameters = manager.decrypt_tool_parameters(agent_tool_entity.tool_parameters or {})
masked_parameter = manager.mask_tool_parameters(parameters or {})
else:
parameters = {}
masked_parameter = {}
key = f"{agent_tool_entity.provider_id}.{agent_tool_entity.provider_type}.{agent_tool_entity.tool_name}"
masked_parameter_map[key] = masked_parameter
parameter_map[key] = parameters
tool_map[key] = tool_runtime
# encrypt agent tool parameters if it's secret-input
agent_mode = new_app_model_config.agent_mode_dict
for tool in agent_mode.get("tools") or []:
agent_tool_entity = AgentToolEntity.model_validate(tool)
# get tool
key = f"{agent_tool_entity.provider_id}.{agent_tool_entity.provider_type}.{agent_tool_entity.tool_name}"
if key in tool_map:
tool_runtime = tool_map[key]
else:
try:
tool_runtime = ToolManager.get_agent_tool_runtime(
tenant_id=current_tenant_id,
app_id=app_model.id,
agent_tool=agent_tool_entity,
user_id=current_user_id,
)
except Exception:
continue
manager = ToolParameterConfigurationManager(
tenant_id=current_tenant_id,
tool_runtime=tool_runtime,
provider_name=agent_tool_entity.provider_id,
provider_type=agent_tool_entity.provider_type,
identity_id=f"AGENT.{app_model.id}",
)
manager.delete_tool_parameters_cache()
# override parameters if it equals to masked parameters
if agent_tool_entity.tool_parameters:
if key not in masked_parameter_map:
continue
for masked_key, masked_value in masked_parameter_map[key].items():
if (
masked_key in agent_tool_entity.tool_parameters
and agent_tool_entity.tool_parameters[masked_key] == masked_value
):
agent_tool_entity.tool_parameters[masked_key] = parameter_map[key].get(masked_key)
# encrypt parameters
if agent_tool_entity.tool_parameters:
tool["tool_parameters"] = manager.encrypt_tool_parameters(agent_tool_entity.tool_parameters or {})
# update app model config
new_app_model_config.agent_mode = json.dumps(agent_mode)
session.add(new_app_model_config)
session.flush()
app_model.app_model_config_id = new_app_model_config.id
app_model.updated_by = current_user_id
app_model.updated_at = naive_utc_now()
app_model_config_was_updated.send(
app_model,
app_model_config=new_app_model_config,
session=session,
)
return {"result": "success"}
@@ -0,0 +1,384 @@
from typing import Literal, NotRequired
from pydantic import BaseModel, ConfigDict, Field, with_config
from typing_extensions import TypedDict
from core.entities.agent_entities import PlanningStrategy
from core.rag.entities.metadata_entities import SupportedComparisonOperator
from core.rag.rerank.rerank_type import RerankMode
from core.tools.entities.tool_entities import ToolProviderType
from graphon.file.enums import FileTransferMethod, FileType
from graphon.model_runtime.entities.llm_entities import LLMMode
# The recursive schema also keeps generated clients from turning JSON values into `any`.
type AppConfigJsonValue = str | int | float | bool | None | list[AppConfigJsonValue] | dict[str, AppConfigJsonValue]
# Mypy does not yet support PEP 728 extra_items; pyrefly and Pydantic check these declarations.
# https://github.com/python/mypy/issues/18176
class AppModelSelectionPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
provider: str
name: str
completion_params: dict[str, AppConfigJsonValue]
mode: NotRequired[str | None]
# These config managers persist nested extensions alongside their known fields.
# Keep those JSON values when an existing configuration is published again.
class AppPromptMessagePayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
text: str
role: str
class AppChatPromptPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
prompt: NotRequired[list[AppPromptMessagePayload]]
class AppConversationRolesPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
user_prefix: str
assistant_prefix: str
class AppCompletionPromptTextPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
text: str
class AppCompletionPromptPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
prompt: NotRequired[AppCompletionPromptTextPayload]
conversation_histories_role: NotRequired[AppConversationRolesPayload]
@with_config(ConfigDict(extra="allow"))
class AppInputFieldBasePayload(TypedDict):
label: str
variable: str
description: NotRequired[str]
required: NotRequired[bool | None]
default: NotRequired[AppConfigJsonValue]
max_length: NotRequired[int | None]
hide: NotRequired[bool]
type: NotRequired[str]
enabled: NotRequired[bool]
config: NotRequired[dict[str, AppConfigJsonValue]]
icon: NotRequired[str | None]
icon_background: NotRequired[str | None]
json_schema: NotRequired[str | dict[str, AppConfigJsonValue] | None]
class AppInputFieldPayload(AppInputFieldBasePayload, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
options: NotRequired[list[str]]
class AppSelectFieldPayload(AppInputFieldBasePayload, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
options: NotRequired[list[str] | None]
class AppTextInputPayload(BaseModel):
model_config = ConfigDict(extra="forbid", serialize_by_alias=True)
text_input: AppInputFieldPayload = Field(alias="text-input")
class AppSelectInputPayload(BaseModel):
model_config = ConfigDict(extra="forbid")
select: AppSelectFieldPayload
class AppParagraphInputPayload(BaseModel):
model_config = ConfigDict(extra="forbid")
paragraph: AppInputFieldPayload
class AppNumberInputPayload(BaseModel):
model_config = ConfigDict(extra="forbid")
number: AppInputFieldPayload
class AppCheckboxInputPayload(BaseModel):
model_config = ConfigDict(extra="forbid")
checkbox: AppInputFieldPayload
class AppExternalDataInputPayload(BaseModel):
model_config = ConfigDict(extra="forbid")
external_data_tool: AppInputFieldPayload
type AppUserInputFormPayload = (
AppTextInputPayload
| AppSelectInputPayload
| AppParagraphInputPayload
| AppNumberInputPayload
| AppCheckboxInputPayload
| AppExternalDataInputPayload
)
class AppFeaturePayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool | None]
class AppSuggestedQuestionsModelPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
provider: str
name: str
mode: NotRequired[LLMMode | Literal[""]]
completion_params: NotRequired[dict[str, AppConfigJsonValue]]
class AppSuggestedQuestionsPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool | None]
prompt: NotRequired[str]
model: NotRequired[AppSuggestedQuestionsModelPayload]
class AppTextToSpeechPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool | None]
voice: NotRequired[str]
language: NotRequired[str]
autoPlay: NotRequired[Literal["enabled", "disabled"]]
class AppModerationContentPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool | None]
preset_response: NotRequired[str | None]
class AppModerationConfigPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
inputs_config: NotRequired[AppModerationContentPayload | None]
outputs_config: NotRequired[AppModerationContentPayload | None]
keywords: NotRequired[str | None]
api_based_extension_id: NotRequired[str | None]
class AppModerationPayload(BaseModel):
enabled: bool | None = None
type: str | None = None
config: AppModerationConfigPayload | None = None
class AppExternalDataToolPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool | None]
type: NotRequired[str]
label: NotRequired[str]
variable: NotRequired[str]
config: NotRequired[dict[str, AppConfigJsonValue]]
icon: NotRequired[str]
icon_background: NotRequired[str]
class AppDatasetSelectionPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
id: NotRequired[str]
enabled: NotRequired[bool]
class AppLegacyDatasetSelectionPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
id: NotRequired[str]
enabled: NotRequired[bool | None]
class AppDatasetToolPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
dataset: AppDatasetSelectionPayload
class AppEmptyDatasetCollectionPayload(TypedDict, closed=True):
pass
class AppDatasetCollectionPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
strategy: NotRequired[str]
datasets: list[AppDatasetToolPayload]
class AppRerankingModelPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
reranking_provider_name: NotRequired[str]
reranking_model_name: NotRequired[str]
class AppVectorWeightPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
vector_weight: float
embedding_provider_name: str
embedding_model_name: str
class AppKeywordWeightPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
keyword_weight: float
class AppRetrievalWeightsPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
weight_type: NotRequired[Literal["semantic_first", "keyword_first", "customized"]]
vector_setting: AppVectorWeightPayload
keyword_setting: AppKeywordWeightPayload
class AppMetadataModelPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
provider: NotRequired[str]
name: NotRequired[str]
mode: NotRequired[LLMMode | Literal[""]]
completion_params: NotRequired[dict[str, AppConfigJsonValue]]
class AppMetadataConditionPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
id: NotRequired[str]
name: str
metadata_id: NotRequired[str]
comparison_operator: SupportedComparisonOperator
value: NotRequired[str | int | float | list[str] | None]
class AppMetadataFilteringPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
logical_operator: NotRequired[Literal["and", "or"] | None]
conditions: NotRequired[list[AppMetadataConditionPayload] | None]
class AppDatasetConfigPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
retrieval_model: NotRequired[Literal["single", "multiple"]]
datasets: NotRequired[AppDatasetCollectionPayload | AppEmptyDatasetCollectionPayload | None]
top_k: NotRequired[int]
score_threshold: NotRequired[float | None]
score_threshold_enabled: NotRequired[bool]
reranking_model: NotRequired[AppRerankingModelPayload | None]
weights: NotRequired[AppRetrievalWeightsPayload | None]
reranking_enabled: NotRequired[bool]
reranking_enable: NotRequired[bool]
reranking_mode: NotRequired[RerankMode]
metadata_filtering_mode: NotRequired[Literal["disabled", "automatic", "manual"]]
metadata_model_config: NotRequired[AppMetadataModelPayload | None]
metadata_filtering_conditions: NotRequired[AppMetadataFilteringPayload | None]
class AppAgentPromptPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
first_prompt: NotRequired[str]
next_iteration: NotRequired[str]
class AppProviderAgentToolPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool | None]
provider_type: ToolProviderType
provider_id: str
provider_name: NotRequired[str]
tool_name: str
tool_label: NotRequired[str]
tool_parameters: dict[str, AppConfigJsonValue]
plugin_unique_identifier: NotRequired[str | None]
credential_id: NotRequired[str | None]
isDeleted: NotRequired[bool]
notAuthor: NotRequired[bool]
class AppLegacyDatasetToolPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
dataset: AppLegacyDatasetSelectionPayload
class AppLegacyGoogleSearchToolPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
google_search: AppFeaturePayload
class AppLegacyWebReaderToolPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
web_reader: AppFeaturePayload
class AppLegacyWikipediaToolPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
wikipedia: AppFeaturePayload
class AppLegacyCurrentDatetimeToolPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
current_datetime: AppFeaturePayload
class AppLegacySensitiveWordConfigPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: bool
words: list[str]
canned_response: str
AppLegacySensitiveWordToolPayload = TypedDict( # type: ignore[misc]
"AppLegacySensitiveWordToolPayload",
{"sensitive-word-avoidance": AppLegacySensitiveWordConfigPayload},
extra_items=AppConfigJsonValue,
)
type AppAgentToolPayload = (
AppProviderAgentToolPayload
| AppLegacyDatasetToolPayload
| AppLegacyGoogleSearchToolPayload
| AppLegacyWebReaderToolPayload
| AppLegacyWikipediaToolPayload
| AppLegacyCurrentDatetimeToolPayload
| AppLegacySensitiveWordToolPayload
)
class AppAgentModePayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool | None]
strategy: NotRequired[PlanningStrategy | Literal["cot", "function-calling", ""] | None]
max_iteration: NotRequired[int]
tools: NotRequired[list[AppAgentToolPayload] | None]
prompt: NotRequired[AppAgentPromptPayload | str | None]
class AppImageUploadPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool]
number_limits: NotRequired[int]
detail: NotRequired[Literal["low", "high"] | None]
transfer_methods: NotRequired[list[FileTransferMethod]]
class AppImageConfigPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
number_limits: NotRequired[int]
detail: NotRequired[Literal["low", "high"] | None]
transfer_methods: NotRequired[list[FileTransferMethod]]
class AppFileTypeUploadPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool | None]
number_limits: NotRequired[int | None]
transfer_methods: NotRequired[list[FileTransferMethod] | None]
class AppFilePreviewPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
mode: NotRequired[str | None]
file_type_list: NotRequired[list[str] | None]
class AppFileUploadPayload(TypedDict, extra_items=AppConfigJsonValue): # type: ignore[call-arg]
enabled: NotRequired[bool]
image: NotRequired[AppImageUploadPayload]
image_config: NotRequired[AppImageConfigPayload | None]
document: NotRequired[AppFileTypeUploadPayload | None]
audio: NotRequired[AppFileTypeUploadPayload | None]
video: NotRequired[AppFileTypeUploadPayload | None]
custom: NotRequired[AppFileTypeUploadPayload | None]
preview_config: NotRequired[AppFilePreviewPayload | None]
allowed_file_types: NotRequired[list[FileType]]
allowed_file_extensions: NotRequired[list[str]]
allowed_file_upload_methods: NotRequired[list[FileTransferMethod]]
number_limits: NotRequired[int]
class AppModelConfigPayload(BaseModel):
"""Write transport; app-mode validators own defaults and feature-specific rules."""
model: AppModelSelectionPayload
prompt_type: Literal["simple", "advanced", ""] | None = None
pre_prompt: str | None = None
chat_prompt_config: AppChatPromptPayload | None = None
completion_prompt_config: AppCompletionPromptPayload | None = None
user_input_form: list[AppUserInputFormPayload] | None = None
dataset_query_variable: str | None = None
dataset_configs: AppDatasetConfigPayload | None = None
agent_mode: AppAgentModePayload | None = None
external_data_tools: list[AppExternalDataToolPayload] | None = None
file_upload: AppFileUploadPayload | None = None
opening_statement: str | None = None
suggested_questions: list[str] | None = None
suggested_questions_after_answer: AppSuggestedQuestionsPayload | None = None
more_like_this: AppFeaturePayload | None = None
speech_to_text: AppFeaturePayload | None = None
text_to_speech: AppTextToSpeechPayload | None = None
retriever_resource: AppFeaturePayload | None = None
sensitive_word_avoidance: AppModerationPayload | None = None
@@ -0,0 +1,327 @@
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import (
AppNotFoundError,
InvalidTracingConfigError,
TracingConfigAlreadyExistsError,
TracingConfigConflictError,
TracingConfigNotFoundError,
TracingConfigProcessingError,
TracingConfigVerificationFailedError,
TracingProviderUnavailableError,
UnsupportedTracingProviderError,
)
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import (
RBACPermission,
model_validate,
)
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from libs.external_api import error_responses
from libs.helper import dump_response
from machinery.context import RequestContext
from models.account import TenantAccountRole
from services.app_tracing_config_service import (
AppTracingConfigAlreadyExistsError,
AppTracingConfigAppNotFoundError,
AppTracingConfigConflictError,
AppTracingConfigInvalidConfigurationError,
AppTracingConfigInvalidProviderError,
AppTracingConfigNotFoundError,
AppTracingConfigProcessingError,
AppTracingConfigProviderUnavailableError,
AppTracingConfigRecord,
AppTracingConfigVerificationFailedError,
)
_APP_TRACING_CONFIG_EDIT_ROLES = frozenset(
{
TenantAccountRole.OWNER,
TenantAccountRole.ADMIN,
TenantAccountRole.EDITOR,
}
)
class TraceProviderQuery(BaseModel):
tracing_provider: str = Field(..., description="Tracing provider name")
class TraceConfigPayload(BaseModel):
activate: bool = Field(
default=False,
description="Save, select this provider, and enable tracing atomically; preserve capture settings",
)
tracing_provider: str = Field(..., description="Tracing provider name")
tracing_config: dict[str, str | None] = Field(
...,
description="Tracing configuration data",
)
class TraceAppConfigResponse(ResponseModel):
result: str | None = None
error: str | None = None
has_not_configured: bool | None = None
id: str | None = None
app_id: str | None = None
tracing_provider: str | None = None
tracing_config: dict[str, str | None] | None = Field(
default=None, description="Non-secret fields; secrets are write-only and never returned"
)
configured_secrets: list[str] | None = Field(
default=None, description="Secret fields that hold a value; submit them empty to keep that value"
)
is_active: bool | None = None
created_at: str | None = None
updated_at: str | None = None
@classmethod
def from_record(cls, record: AppTracingConfigRecord) -> "TraceAppConfigResponse":
return cls(
id=record.id,
app_id=record.app_id,
tracing_provider=record.tracing_provider,
tracing_config=record.tracing_config,
configured_secrets=list(record.configured_secrets),
is_active=record.is_active,
created_at=str(record.created_at),
updated_at=str(record.updated_at),
)
register_schema_models(console_ns, TraceProviderQuery, TraceConfigPayload)
register_response_schema_models(console_ns, TraceAppConfigResponse)
@console_ns.route("/apps/<uuid:app_id>/trace-config")
class TraceAppConfigApi(Resource):
"""
Manage trace app configurations
"""
@error_responses(
AppNotFoundError, TracingConfigProcessingError, TracingProviderUnavailableError, UnsupportedTracingProviderError
)
@console_ns.doc("get_trace_app_config")
@console_ns.doc(description="Get tracing configuration for an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(TraceProviderQuery))
@console_ns.response(
200,
"Tracing configuration retrieved successfully",
console_ns.models[TraceAppConfigResponse.__name__],
)
@console_ns.response(400, "Invalid request parameters or unsupported tracing provider")
@console_ns.response(404, "Application not found")
@console_ns.response(500, "Tracing configuration processing failed")
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_TRACING_CONFIG, PlainApp())],
)
@model_validate(TraceProviderQuery)
def get(
self,
req_data: TraceProviderQuery,
request_context: RequestContext,
app_id: UUID,
):
try:
trace_config = application_services().app_tracing_configs.get(
context=request_context,
app_id=str(app_id),
tracing_provider=req_data.tracing_provider,
)
except AppTracingConfigAppNotFoundError as error:
raise AppNotFoundError() from error
except AppTracingConfigInvalidProviderError as error:
raise UnsupportedTracingProviderError() from error
except AppTracingConfigProviderUnavailableError as error:
raise TracingProviderUnavailableError() from error
except AppTracingConfigProcessingError as error:
raise TracingConfigProcessingError() from error
except ValueError as error:
raise TracingConfigProcessingError() from error
if trace_config is None:
return dump_response(TraceAppConfigResponse, {"has_not_configured": True}, exclude_none=True)
return dump_response(
TraceAppConfigResponse,
TraceAppConfigResponse.from_record(trace_config),
exclude_none=True,
)
@error_responses(
AppNotFoundError,
InvalidTracingConfigError,
TracingConfigAlreadyExistsError,
TracingConfigProcessingError,
TracingConfigVerificationFailedError,
TracingProviderUnavailableError,
UnsupportedTracingProviderError,
)
@console_ns.doc("create_trace_app_config")
@console_ns.doc(description="Create a new tracing configuration for an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[TraceConfigPayload.__name__])
@console_ns.response(
201,
"Tracing configuration created successfully",
console_ns.models[TraceAppConfigResponse.__name__],
)
@console_ns.response(400, "Invalid request parameters or tracing configuration")
@console_ns.response(403, "Insufficient permissions")
@console_ns.response(404, "Application not found")
@console_ns.response(409, "Tracing configuration already exists")
@console_ns.response(500, "Tracing configuration processing failed")
@console_account_admission(
allowed_roles=_APP_TRACING_CONFIG_EDIT_ROLES,
rbac_checks=[RBACCheck(RBACPermission.APP_TRACING_CONFIG, PlainApp())],
)
@model_validate(TraceConfigPayload)
def post(
self,
req_data: TraceConfigPayload,
request_context: RequestContext,
app_id: UUID,
):
"""Create a new trace app configuration"""
try:
application_services().app_tracing_configs.create(
context=request_context,
app_id=str(app_id),
tracing_provider=req_data.tracing_provider,
tracing_config=req_data.tracing_config,
activate=req_data.activate,
)
except AppTracingConfigAppNotFoundError as error:
raise AppNotFoundError() from error
except AppTracingConfigAlreadyExistsError as error:
raise TracingConfigAlreadyExistsError() from error
except AppTracingConfigInvalidProviderError as error:
raise UnsupportedTracingProviderError() from error
except AppTracingConfigProviderUnavailableError as error:
raise TracingProviderUnavailableError() from error
except AppTracingConfigInvalidConfigurationError as error:
raise InvalidTracingConfigError() from error
except AppTracingConfigVerificationFailedError as error:
raise TracingConfigVerificationFailedError() from error
except AppTracingConfigProcessingError as error:
raise TracingConfigProcessingError() from error
except ValueError as error:
raise TracingConfigProcessingError() from error
return dump_response(TraceAppConfigResponse, {"result": "success"}, exclude_none=True), 201
@error_responses(
AppNotFoundError,
InvalidTracingConfigError,
TracingConfigNotFoundError,
TracingConfigConflictError,
TracingConfigProcessingError,
TracingConfigVerificationFailedError,
TracingProviderUnavailableError,
UnsupportedTracingProviderError,
)
@console_ns.doc("update_trace_app_config")
@console_ns.doc(description="Update an existing tracing configuration for an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[TraceConfigPayload.__name__])
@console_ns.response(
200,
"Tracing configuration updated successfully",
console_ns.models[TraceAppConfigResponse.__name__],
)
@console_ns.response(400, "Invalid request parameters or tracing configuration")
@console_ns.response(403, "Insufficient permissions")
@console_ns.response(404, "Application or tracing configuration not found")
@console_ns.response(500, "Tracing configuration processing failed")
@console_account_admission(
allowed_roles=_APP_TRACING_CONFIG_EDIT_ROLES,
rbac_checks=[RBACCheck(RBACPermission.APP_TRACING_CONFIG, PlainApp())],
)
@model_validate(TraceConfigPayload)
def patch(
self,
req_data: TraceConfigPayload,
request_context: RequestContext,
app_id: UUID,
):
"""Update an existing trace app configuration"""
try:
application_services().app_tracing_configs.update(
context=request_context,
app_id=str(app_id),
tracing_provider=req_data.tracing_provider,
tracing_config=req_data.tracing_config,
activate=req_data.activate,
)
except AppTracingConfigAppNotFoundError as error:
raise AppNotFoundError() from error
except AppTracingConfigConflictError as error:
raise TracingConfigConflictError() from error
except AppTracingConfigNotFoundError as error:
raise TracingConfigNotFoundError() from error
except AppTracingConfigInvalidProviderError as error:
raise UnsupportedTracingProviderError() from error
except AppTracingConfigProviderUnavailableError as error:
raise TracingProviderUnavailableError() from error
except AppTracingConfigInvalidConfigurationError as error:
raise InvalidTracingConfigError() from error
except AppTracingConfigVerificationFailedError as error:
raise TracingConfigVerificationFailedError() from error
except AppTracingConfigProcessingError as error:
raise TracingConfigProcessingError() from error
except ValueError as error:
raise TracingConfigProcessingError() from error
return dump_response(TraceAppConfigResponse, {"result": "success"}, exclude_none=True)
@error_responses(
AppNotFoundError, TracingConfigNotFoundError, TracingConfigProcessingError, UnsupportedTracingProviderError
)
@console_ns.doc("delete_trace_app_config")
@console_ns.doc(description="Delete an existing tracing configuration for an application")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(TraceProviderQuery))
@console_ns.response(204, "Tracing configuration deleted successfully")
@console_ns.response(400, "Invalid request parameters or unsupported tracing provider")
@console_ns.response(403, "Insufficient permissions")
@console_ns.response(404, "Application or tracing configuration not found")
@console_ns.response(500, "Tracing configuration processing failed")
@console_account_admission(
allowed_roles=_APP_TRACING_CONFIG_EDIT_ROLES,
rbac_checks=[RBACCheck(RBACPermission.APP_TRACING_CONFIG, PlainApp())],
)
@model_validate(TraceProviderQuery)
def delete(
self,
req_data: TraceProviderQuery,
request_context: RequestContext,
app_id: UUID,
):
"""Delete an existing trace app configuration"""
try:
application_services().app_tracing_configs.delete(
context=request_context,
app_id=str(app_id),
tracing_provider=req_data.tracing_provider,
)
except AppTracingConfigAppNotFoundError as error:
raise AppNotFoundError() from error
except AppTracingConfigNotFoundError as error:
raise TracingConfigNotFoundError() from error
except AppTracingConfigInvalidProviderError as error:
raise UnsupportedTracingProviderError() from error
except AppTracingConfigProcessingError as error:
raise TracingConfigProcessingError() from error
except ValueError as error:
raise TracingConfigProcessingError() from error
return "", 204
@@ -0,0 +1,9 @@
from extensions.ext_application_services import application_services
from services.rbac.contracts import RBACResourceType
def get_app_permission_keys(tenant_id: str, account_id: str | None, app_id: str) -> list[str]:
permission_keys_map = application_services().rbac.members.resource_permissions(
tenant_id, account_id, RBACResourceType.APP, [app_id]
)
return permission_keys_map.get(app_id, [])
@@ -0,0 +1,27 @@
"""Public catalog preview admission, separate from authenticated trial execution."""
from collections.abc import Callable
from functools import wraps
from typing import Concatenate
from controllers.console.app.error import AppNotFoundError
from extensions.ext_application_services import application_services
from libs.external_api import document_errors
from services.app_preview_query_service import AppPreviewRef, AppPreviewUnavailableError
def get_preview_app[T, **P, R](
view: Callable[Concatenate[T, AppPreviewRef, P], R],
) -> Callable[Concatenate[T, P], R]:
@wraps(view)
def decorated(self: T, /, *args: P.args, **kwargs: P.kwargs) -> R:
app_id = kwargs.pop("app_id", None)
if app_id is None:
raise RuntimeError("The app preview admission route must provide app_id")
try:
app = application_services().app_previews.get_access(app_id=str(app_id))
except AppPreviewUnavailableError as error:
raise AppNotFoundError() from error
return view(self, app, *args, **kwargs)
return document_errors(decorated, AppNotFoundError)
@@ -0,0 +1,156 @@
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from werkzeug.exceptions import NotFound
from constants.languages import supported_language
from controllers.common.rbac import AgentBehindApp, PlainApp, RBACCheck
from controllers.common.schema import register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AppNotFoundError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import (
RBACPermission,
model_validate,
)
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from libs.external_api import error_responses
from libs.helper import dump_response
from machinery.context import RequestContext
from models.account import TenantAccountRole
from services.app_site_service import (
AppSiteAppNotFoundError,
AppSiteChanges,
AppSiteNotFoundError,
AppSiteTokenStrategy,
)
_APP_SITE_EDIT_ROLES = frozenset(
{
TenantAccountRole.OWNER,
TenantAccountRole.ADMIN,
TenantAccountRole.EDITOR,
}
)
_APP_SITE_TOKEN_RESET_ROLES = frozenset(
{
TenantAccountRole.OWNER,
TenantAccountRole.ADMIN,
}
)
class AppSiteUpdatePayload(BaseModel):
title: str | None = Field(default=None)
icon_type: str | None = Field(default=None)
icon: str | None = Field(default=None)
icon_background: str | None = Field(default=None)
description: str | None = Field(default=None)
default_language: str | None = Field(default=None)
chat_color_theme: str | None = Field(default=None)
chat_color_theme_inverted: bool | None = Field(default=None)
customize_domain: str | None = Field(default=None)
copyright: str | None = Field(default=None)
privacy_policy: str | None = Field(default=None)
input_placeholder: str | None = Field(default=None)
custom_disclaimer: str | None = Field(default=None)
customize_token_strategy: AppSiteTokenStrategy | None = Field(default=None)
prompt_public: bool | None = Field(default=None)
show_workflow_steps: bool | None = Field(default=None)
use_icon_as_answer_icon: bool | None = Field(default=None)
@field_validator("default_language")
@classmethod
def validate_language(cls, value: str | None) -> str | None:
if value is None:
return value
return supported_language(value)
def to_changes(self) -> AppSiteChanges:
return AppSiteChanges(**self.model_dump())
class AppSiteResponse(ResponseModel):
app_id: str
access_token: str | None = Field(default=None, validation_alias="code")
code: str | None = None
title: str
icon: str | None = None
icon_background: str | None = None
description: str | None = None
default_language: str
customize_domain: str | None = None
copyright: str | None = None
privacy_policy: str | None = None
input_placeholder: str | None = None
custom_disclaimer: str | None = None
customize_token_strategy: str
prompt_public: bool
show_workflow_steps: bool
use_icon_as_answer_icon: bool
register_schema_models(console_ns, AppSiteUpdatePayload, AppSiteResponse)
@console_ns.route("/apps/<uuid:app_id>/site")
class AppSite(Resource):
@error_responses(AppNotFoundError)
@console_ns.doc("update_app_site")
@console_ns.doc(description="Update application site configuration")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[AppSiteUpdatePayload.__name__])
@console_ns.response(200, "Site configuration updated successfully", console_ns.models[AppSiteResponse.__name__])
@console_ns.response(403, "Insufficient permissions")
@console_ns.response(404, "App not found")
@console_account_admission(
allowed_roles=_APP_SITE_EDIT_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_RELEASE_AND_VERSION, PlainApp()),
RBACCheck(RBACPermission.AGENT_ACCESS_POINT_MANAGE, AgentBehindApp()),
],
)
@model_validate(AppSiteUpdatePayload)
def post(
self,
req_data: AppSiteUpdatePayload,
request_context: RequestContext,
app_id: UUID,
):
try:
site = application_services().app_sites.update(request_context, str(app_id), req_data.to_changes())
except AppSiteAppNotFoundError as error:
raise AppNotFoundError() from error
except AppSiteNotFoundError as error:
raise NotFound from error
return dump_response(AppSiteResponse, site)
@console_ns.route("/apps/<uuid:app_id>/site/access-token-reset")
class AppSiteAccessTokenReset(Resource):
@error_responses(AppNotFoundError)
@console_ns.doc("reset_app_site_access_token")
@console_ns.doc(description="Reset access token for application site")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(200, "Access token reset successfully", console_ns.models[AppSiteResponse.__name__])
@console_ns.response(403, "Insufficient permissions (admin/owner required)")
@console_ns.response(404, "App or site not found")
@console_account_admission(
allowed_roles=_APP_SITE_TOKEN_RESET_ROLES,
rbac_checks=[
RBACCheck(RBACPermission.APP_RELEASE_AND_VERSION, PlainApp()),
RBACCheck(RBACPermission.AGENT_ACCESS_POINT_MANAGE, AgentBehindApp()),
],
)
def post(self, request_context: RequestContext, app_id: UUID):
try:
site = application_services().app_sites.reset_access_token(request_context, str(app_id))
except AppSiteAppNotFoundError as error:
raise AppNotFoundError() from error
except AppSiteNotFoundError as error:
raise NotFound from error
return dump_response(AppSiteResponse, site)
@@ -0,0 +1,424 @@
from datetime import datetime
from decimal import Decimal
from http import HTTPStatus
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from controllers.common.errors import InvalidArgumentError
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AppNotFoundError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import RBACPermission, model_validate
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from libs.datetime_utils import parse_time_range
from libs.external_api import error_responses
from libs.helper import dump_response
from libs.login import current_account_with_tenant
from machinery.context import RequestContext
from models.model import AppMode
from services.app.console_service import ConsoleAppNotFoundError
class StatisticTimeRangeQuery(BaseModel):
start: str | None = Field(default=None, description="Start date (YYYY-MM-DD HH:MM)")
end: str | None = Field(default=None, description="End date (YYYY-MM-DD HH:MM)")
@field_validator("start", "end", mode="before")
@classmethod
def empty_string_to_none(cls, value: str | None) -> str | None:
if value == "":
return None
return value
class DailyMessageStatisticItem(ResponseModel):
date: str
message_count: int
register_schema_models(console_ns, StatisticTimeRangeQuery)
class StatisticDataResponse[T](ResponseModel):
data: list[T]
class DailyMessageStatisticResponse(StatisticDataResponse[DailyMessageStatisticItem]):
pass
class DailyConversationStatisticItem(ResponseModel):
date: str
conversation_count: int
class DailyConversationStatisticResponse(StatisticDataResponse[DailyConversationStatisticItem]):
pass
class DailyTerminalStatisticItem(ResponseModel):
date: str
terminal_count: int
class DailyTerminalStatisticResponse(StatisticDataResponse[DailyTerminalStatisticItem]):
pass
class DailyTokenCostStatisticItem(ResponseModel):
date: str
token_count: int | None = None
total_price: Decimal | None = None
currency: str | None = None
class DailyTokenCostStatisticResponse(StatisticDataResponse[DailyTokenCostStatisticItem]):
pass
class AverageSessionInteractionStatisticItem(ResponseModel):
date: str
interactions: float
class AverageSessionInteractionStatisticResponse(StatisticDataResponse[AverageSessionInteractionStatisticItem]):
pass
class UserSatisfactionRateStatisticItem(ResponseModel):
date: str
rate: float
class UserSatisfactionRateStatisticResponse(StatisticDataResponse[UserSatisfactionRateStatisticItem]):
pass
class AverageResponseTimeStatisticItem(ResponseModel):
date: str
latency: float
class AverageResponseTimeStatisticResponse(StatisticDataResponse[AverageResponseTimeStatisticItem]):
pass
class TokensPerSecondStatisticItem(ResponseModel):
date: str
tps: float
class TokensPerSecondStatisticResponse(StatisticDataResponse[TokensPerSecondStatisticItem]):
pass
register_response_schema_models(
console_ns,
DailyMessageStatisticItem,
DailyMessageStatisticResponse,
DailyConversationStatisticItem,
DailyConversationStatisticResponse,
DailyTerminalStatisticItem,
DailyTerminalStatisticResponse,
DailyTokenCostStatisticItem,
DailyTokenCostStatisticResponse,
AverageSessionInteractionStatisticItem,
AverageSessionInteractionStatisticResponse,
UserSatisfactionRateStatisticItem,
UserSatisfactionRateStatisticResponse,
AverageResponseTimeStatisticItem,
AverageResponseTimeStatisticResponse,
TokensPerSecondStatisticItem,
TokensPerSecondStatisticResponse,
)
def _resolve_statistic_time_range(
req_data: StatisticTimeRangeQuery,
) -> tuple[datetime | None, datetime | None, str]:
timezone = current_account_with_tenant().account.timezone
assert timezone is not None
try:
start_date, end_date = parse_time_range(req_data.start, req_data.end, timezone)
except ValueError as error:
raise InvalidArgumentError(str(error)) from error
return start_date, end_date, timezone
@console_ns.route("/apps/<uuid:app_id>/statistics/daily-messages")
class DailyMessageStatistic(Resource):
@console_ns.doc("get_daily_message_statistics")
@console_ns.doc(description="Get daily message statistics for an application")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})
@console_ns.response(
HTTPStatus.OK,
"Daily message statistics retrieved successfully",
console_ns.models[DailyMessageStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(StatisticTimeRangeQuery)
def get(
self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID
) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().app_statistics.get_daily_messages(
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(DailyMessageStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/statistics/daily-conversations")
class DailyConversationStatistic(Resource):
@console_ns.doc("get_daily_conversation_statistics")
@console_ns.doc(description="Get daily conversation statistics for an application")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})
@console_ns.response(
HTTPStatus.OK,
"Daily conversation statistics retrieved successfully",
console_ns.models[DailyConversationStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(StatisticTimeRangeQuery)
def get(
self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID
) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().app_statistics.get_daily_conversations(
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(DailyConversationStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/statistics/daily-end-users")
class DailyTerminalsStatistic(Resource):
@console_ns.doc("get_daily_terminals_statistics")
@console_ns.doc(description="Get daily terminal/end-user statistics for an application")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})
@console_ns.response(
HTTPStatus.OK,
"Daily terminal statistics retrieved successfully",
console_ns.models[DailyTerminalStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(StatisticTimeRangeQuery)
def get(
self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID
) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().app_statistics.get_daily_terminals(
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(DailyTerminalStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/statistics/token-costs")
class DailyTokenCostStatistic(Resource):
@console_ns.doc("get_daily_token_cost_statistics")
@console_ns.doc(description="Get daily token cost statistics for an application")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})
@console_ns.response(
HTTPStatus.OK,
"Daily token cost statistics retrieved successfully",
console_ns.models[DailyTokenCostStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(StatisticTimeRangeQuery)
def get(
self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID
) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().app_statistics.get_daily_token_costs(
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(DailyTokenCostStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/statistics/average-session-interactions")
class AverageSessionInteractionStatistic(Resource):
@console_ns.doc("get_average_session_interaction_statistics")
@console_ns.doc(description="Get average session interaction statistics for an application")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})
@console_ns.response(
HTTPStatus.OK,
"Average session interaction statistics retrieved successfully",
console_ns.models[AverageSessionInteractionStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(StatisticTimeRangeQuery)
def get(
self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID
) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
supported_modes = (AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT)
if app.mode not in supported_modes:
mode_values = {mode.value for mode in supported_modes}
raise AppNotFoundError(f"App mode is not in the supported list: {mode_values}")
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().app_statistics.get_average_session_interactions(
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(AverageSessionInteractionStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/statistics/user-satisfaction-rate")
class UserSatisfactionRateStatistic(Resource):
@console_ns.doc("get_user_satisfaction_rate_statistics")
@console_ns.doc(description="Get user satisfaction rate statistics for an application")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})
@console_ns.response(
HTTPStatus.OK,
"User satisfaction rate statistics retrieved successfully",
console_ns.models[UserSatisfactionRateStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(StatisticTimeRangeQuery)
def get(
self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID
) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().app_statistics.get_user_satisfaction_rates(
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(UserSatisfactionRateStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/statistics/average-response-time")
class AverageResponseTimeStatistic(Resource):
@console_ns.doc("get_average_response_time_statistics")
@console_ns.doc(description="Get average response time statistics for an application")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})
@console_ns.response(
HTTPStatus.OK,
"Average response time statistics retrieved successfully",
console_ns.models[AverageResponseTimeStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(StatisticTimeRangeQuery)
def get(
self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID
) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
supported_modes = (AppMode.COMPLETION,)
if app.mode not in supported_modes:
mode_values = {mode.value for mode in supported_modes}
raise AppNotFoundError(f"App mode is not in the supported list: {mode_values}")
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().app_statistics.get_average_response_times(
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(AverageResponseTimeStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/statistics/tokens-per-second")
class TokensPerSecondStatistic(Resource):
@console_ns.doc("get_tokens_per_second_statistics")
@console_ns.doc(description="Get tokens per second statistics for an application")
@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})
@console_ns.response(
HTTPStatus.OK,
"Tokens per second statistics retrieved successfully",
console_ns.models[TokensPerSecondStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(StatisticTimeRangeQuery)
def get(
self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID
) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().app_statistics.get_tokens_per_second(
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(TokensPerSecondStatisticResponse, {"data": response_data})
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,157 @@
from datetime import datetime
from typing import Any
from dateutil.parser import isoparse
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_schema_models
from controllers.console import console_ns
from controllers.console.app.wraps import get_app_model
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import RBACPermission, model_validate
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from fields.end_user_fields import SimpleEndUser
from fields.member_fields import SimpleAccount
from graphon.enums import WorkflowExecutionStatus
from libs.helper import dump_response, to_timestamp
from machinery.context import RequestContext
from models import App
from models.model import AppMode
class WorkflowAppLogQuery(BaseModel):
keyword: str | None = Field(default=None, description="Search keyword for filtering logs")
status: WorkflowExecutionStatus | None = Field(default=None, description="Execution status filter")
created_at__before: datetime | None = Field(default=None, description="Filter logs created before this timestamp")
created_at__after: datetime | None = Field(default=None, description="Filter logs created after this timestamp")
created_by_end_user_session_id: str | None = Field(default=None, description="Filter by end user session ID")
created_by_account: str | None = Field(default=None, description="Filter by account")
detail: bool = Field(default=False, description="Whether to return detailed logs")
page: int = Field(default=1, ge=1, le=99999, description="Page number (1-99999)")
limit: int = Field(default=20, ge=1, le=100, description="Number of items per page (1-100)")
@field_validator("created_at__before", "created_at__after", mode="before")
@classmethod
def parse_datetime(cls, value: str | None) -> datetime | None:
if value in (None, ""):
return None
return isoparse(value) # type: ignore
@field_validator("detail", mode="before")
@classmethod
def parse_bool(cls, value: bool | str | None) -> bool:
if isinstance(value, bool):
return value
if value is None:
return False
lowered = value.lower()
if lowered in {"1", "true", "yes", "on"}:
return True
if lowered in {"0", "false", "no", "off"}:
return False
raise ValueError("Invalid boolean value for detail")
class WorkflowRunForLogResponse(ResponseModel):
id: str
version: str | None = None
status: str | None = None
triggered_from: str | None = None
error: str | None = None
elapsed_time: float | None = None
total_tokens: int | None = None
total_steps: int | None = None
created_at: int | None = None
finished_at: int | None = None
exceptions_count: int | None = None
@field_validator("status", mode="before")
@classmethod
def _normalize_status(cls, value: Any) -> str | None:
if value is None:
return None
if isinstance(value, str):
return value
return str(getattr(value, "value", value))
@field_validator("created_at", "finished_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class WorkflowAppLogPartialResponse(ResponseModel):
id: str
workflow_run: WorkflowRunForLogResponse | None = None
details: Any = None
created_from: str | None = None
created_by_role: str | None = None
created_by_account: SimpleAccount | None = None
created_by_end_user: SimpleEndUser | None = None
created_at: int | None = None
@field_validator("created_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class WorkflowAppLogPaginationResponse(ResponseModel):
page: int
limit: int
total: int
has_more: bool
data: list[WorkflowAppLogPartialResponse]
register_schema_models(
console_ns,
WorkflowAppLogQuery,
WorkflowRunForLogResponse,
WorkflowAppLogPartialResponse,
WorkflowAppLogPaginationResponse,
)
@console_ns.route("/apps/<uuid:app_id>/workflow-app-logs")
class WorkflowAppLogApi(Resource):
@console_ns.doc("get_workflow_app_logs")
@console_ns.doc(description="Get workflow application execution logs")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(WorkflowAppLogQuery))
@console_ns.response(
200,
"Workflow app logs retrieved successfully",
console_ns.models[WorkflowAppLogPaginationResponse.__name__],
)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_LOG_AND_ANNOTATION, PlainApp())],
)
@get_app_model(mode=[AppMode.WORKFLOW])
@model_validate(WorkflowAppLogQuery)
def get(
self,
req_data: WorkflowAppLogQuery,
_request_context: RequestContext,
app_model: App,
):
"""
Get workflow app logs
"""
result = application_services().workflow_app_logs.list_logs(
tenant_id=app_model.tenant_id,
app_id=app_model.id,
keyword=req_data.keyword,
status=req_data.status,
created_at_before=req_data.created_at__before,
created_at_after=req_data.created_at__after,
page=req_data.page,
limit=req_data.limit,
detail=req_data.detail,
created_by_end_user_session_id=req_data.created_by_end_user_session_id,
created_by_account=req_data.created_by_account,
)
return dump_response(WorkflowAppLogPaginationResponse, result)
@@ -0,0 +1,450 @@
from collections.abc import Generator
from contextlib import contextmanager
from datetime import datetime
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field, computed_field, field_validator
from controllers.common.errors import ForbiddenError, InvalidArgumentError, NotFoundError
from controllers.common.schema import register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AppNotFoundError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import model_validate
from enums.account import TenantAccountRole
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from fields.member_fields import AccountWithRole
from libs.external_api import error_responses
from libs.helper import build_avatar_url, dump_response, to_timestamp
from machinery.context import RequestContext
from services.app.workflow_comment_service import (
InvalidMentionedUserIdError,
InvalidWorkflowCommentContentError,
WorkflowCommentAppNotFoundError,
WorkflowCommentDraft,
WorkflowCommentEdit,
WorkflowCommentNotFoundError,
WorkflowCommentPermissionError,
WorkflowCommentReplyDraft,
WorkflowCommentReplyNotFoundError,
)
# Same roles as the legacy `edit_permission_required` gate; like it, admission skips the role check under RBAC.
_EDIT_ROLES = frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR})
class WorkflowCommentCreatePayload(BaseModel):
content: str = Field(..., description="Comment content")
position_x: float = Field(..., description="Comment X position")
position_y: float = Field(..., description="Comment Y position")
mentioned_user_ids: list[str] = Field(default_factory=list, description="Mentioned user IDs")
class WorkflowCommentUpdatePayload(BaseModel):
content: str = Field(..., description="Comment content")
position_x: float | None = Field(default=None, description="Comment X position")
position_y: float | None = Field(default=None, description="Comment Y position")
mentioned_user_ids: list[str] | None = Field(
default=None,
description="Mentioned user IDs. Omit to keep existing mentions.",
)
class WorkflowCommentReplyPayload(BaseModel):
content: str = Field(..., description="Reply content")
mentioned_user_ids: list[str] = Field(default_factory=list, description="Mentioned user IDs")
class WorkflowCommentMentionUsersPayload(BaseModel):
users: list[AccountWithRole]
class WorkflowCommentAccount(ResponseModel):
id: str
name: str
email: str
avatar: str | None = Field(default=None, exclude=True)
@computed_field(return_type=str | None) # type: ignore[prop-decorator]
@property
def avatar_url(self) -> str | None:
return build_avatar_url(self.avatar)
class WorkflowCommentReply(ResponseModel):
id: str
content: str
created_by: str
created_by_account: WorkflowCommentAccount | None = None
created_at: int | None = None
@field_validator("created_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class WorkflowCommentMention(ResponseModel):
mentioned_user_id: str
mentioned_user_account: WorkflowCommentAccount | None = None
reply_id: str | None = None
class WorkflowCommentBasic(ResponseModel):
id: str
position_x: float
position_y: float
content: str
created_by: str
created_by_account: WorkflowCommentAccount | None = None
created_at: int | None = None
updated_at: int | None = None
resolved: bool
resolved_at: int | None = None
resolved_by: str | None = None
resolved_by_account: WorkflowCommentAccount | None = None
reply_count: int
mention_count: int
participants: list[WorkflowCommentAccount]
@field_validator("created_at", "updated_at", "resolved_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class WorkflowCommentBasicList(ResponseModel):
data: list[WorkflowCommentBasic]
class WorkflowCommentDetail(ResponseModel):
id: str
position_x: float
position_y: float
content: str
created_by: str
created_by_account: WorkflowCommentAccount | None = None
created_at: int | None = None
updated_at: int | None = None
resolved: bool
resolved_at: int | None = None
resolved_by: str | None = None
resolved_by_account: WorkflowCommentAccount | None = None
replies: list[WorkflowCommentReply]
mentions: list[WorkflowCommentMention]
@field_validator("created_at", "updated_at", "resolved_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class WorkflowCommentCreate(ResponseModel):
id: str
created_at: int | None = None
@field_validator("created_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class WorkflowCommentUpdate(ResponseModel):
id: str
updated_at: int | None = None
@field_validator("updated_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class WorkflowCommentResolve(ResponseModel):
id: str
resolved: bool
resolved_at: int | None = None
resolved_by: str | None = None
@field_validator("resolved_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class WorkflowCommentReplyCreate(ResponseModel):
id: str
created_at: int | None = None
@field_validator("created_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
class WorkflowCommentReplyUpdate(ResponseModel):
id: str
updated_at: int | None = None
@field_validator("updated_at", mode="before")
@classmethod
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
return to_timestamp(value)
register_schema_models(
console_ns,
WorkflowCommentCreatePayload,
WorkflowCommentUpdatePayload,
WorkflowCommentReplyPayload,
)
register_response_schema_models(
console_ns,
AccountWithRole,
WorkflowCommentMentionUsersPayload,
WorkflowCommentAccount,
WorkflowCommentReply,
WorkflowCommentMention,
WorkflowCommentBasic,
WorkflowCommentBasicList,
WorkflowCommentDetail,
WorkflowCommentCreate,
WorkflowCommentUpdate,
WorkflowCommentResolve,
WorkflowCommentReplyCreate,
WorkflowCommentReplyUpdate,
)
@contextmanager
def _translate_errors() -> Generator[None]:
"""Map framework-neutral comment failures back to the existing transport errors."""
try:
yield
except WorkflowCommentAppNotFoundError as error:
raise AppNotFoundError() from error
except (WorkflowCommentNotFoundError, WorkflowCommentReplyNotFoundError) as error:
raise NotFoundError(str(error)) from error
except WorkflowCommentPermissionError as error:
raise ForbiddenError(str(error)) from error
except (InvalidWorkflowCommentContentError, InvalidMentionedUserIdError) as error:
raise InvalidArgumentError(str(error)) from error
@console_ns.route("/apps/<uuid:app_id>/workflow/comments")
class WorkflowCommentListApi(Resource):
"""API for listing and creating workflow comments."""
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("list_workflow_comments")
@console_ns.doc(description="Get all comments for a workflow")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(200, "Comments retrieved successfully", console_ns.models[WorkflowCommentBasicList.__name__])
@console_account_admission()
def get(self, request_context: RequestContext, app_id: UUID):
"""Get all comments for a workflow."""
with _translate_errors():
comments = application_services().workflow_comments.list_comments(request_context, str(app_id))
return dump_response(WorkflowCommentBasicList, {"data": comments})
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("create_workflow_comment")
@console_ns.doc(description="Create a new workflow comment")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.expect(console_ns.models[WorkflowCommentCreatePayload.__name__])
@console_ns.response(201, "Comment created successfully", console_ns.models[WorkflowCommentCreate.__name__])
@console_account_admission(allowed_roles=_EDIT_ROLES)
@model_validate(WorkflowCommentCreatePayload)
def post(self, req_data: WorkflowCommentCreatePayload, request_context: RequestContext, app_id: UUID):
"""Create a new workflow comment."""
with _translate_errors():
result = application_services().workflow_comments.create_comment(
request_context,
str(app_id),
WorkflowCommentDraft(
content=req_data.content,
position_x=req_data.position_x,
position_y=req_data.position_y,
mentioned_user_ids=tuple(req_data.mentioned_user_ids),
),
)
return dump_response(WorkflowCommentCreate, result), 201
@console_ns.route("/apps/<uuid:app_id>/workflow/comments/<string:comment_id>")
class WorkflowCommentDetailApi(Resource):
"""API for managing individual workflow comments."""
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("get_workflow_comment")
@console_ns.doc(description="Get a specific workflow comment")
@console_ns.doc(params={"app_id": "Application ID", "comment_id": "Comment ID"})
@console_ns.response(200, "Comment retrieved successfully", console_ns.models[WorkflowCommentDetail.__name__])
@console_account_admission()
def get(self, request_context: RequestContext, app_id: UUID, comment_id: str):
"""Get a specific workflow comment."""
with _translate_errors():
comment = application_services().workflow_comments.get_comment(request_context, str(app_id), comment_id)
return dump_response(WorkflowCommentDetail, comment)
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("update_workflow_comment")
@console_ns.doc(description="Update a workflow comment")
@console_ns.doc(params={"app_id": "Application ID", "comment_id": "Comment ID"})
@console_ns.expect(console_ns.models[WorkflowCommentUpdatePayload.__name__])
@console_ns.response(200, "Comment updated successfully", console_ns.models[WorkflowCommentUpdate.__name__])
@console_account_admission(allowed_roles=_EDIT_ROLES)
@model_validate(WorkflowCommentUpdatePayload)
def put(
self,
req_data: WorkflowCommentUpdatePayload,
request_context: RequestContext,
app_id: UUID,
comment_id: str,
):
"""Update a workflow comment."""
mentioned_user_ids = req_data.mentioned_user_ids
with _translate_errors():
result = application_services().workflow_comments.update_comment(
request_context,
str(app_id),
comment_id,
WorkflowCommentEdit(
content=req_data.content,
position_x=req_data.position_x,
position_y=req_data.position_y,
mentioned_user_ids=tuple(mentioned_user_ids) if mentioned_user_ids is not None else None,
),
)
return dump_response(WorkflowCommentUpdate, result)
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("delete_workflow_comment")
@console_ns.doc(description="Delete a workflow comment")
@console_ns.doc(params={"app_id": "Application ID", "comment_id": "Comment ID"})
@console_ns.response(204, "Comment deleted successfully")
@console_account_admission(allowed_roles=_EDIT_ROLES)
def delete(self, request_context: RequestContext, app_id: UUID, comment_id: str):
"""Delete a workflow comment."""
with _translate_errors():
application_services().workflow_comments.delete_comment(request_context, str(app_id), comment_id)
return "", 204
@console_ns.route("/apps/<uuid:app_id>/workflow/comments/<string:comment_id>/resolve")
class WorkflowCommentResolveApi(Resource):
"""API for resolving and reopening workflow comments."""
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("resolve_workflow_comment")
@console_ns.doc(description="Resolve a workflow comment")
@console_ns.doc(params={"app_id": "Application ID", "comment_id": "Comment ID"})
@console_ns.response(200, "Comment resolved successfully", console_ns.models[WorkflowCommentResolve.__name__])
@console_account_admission(allowed_roles=_EDIT_ROLES)
def post(self, request_context: RequestContext, app_id: UUID, comment_id: str):
"""Resolve a workflow comment."""
with _translate_errors():
result = application_services().workflow_comments.resolve_comment(request_context, str(app_id), comment_id)
return dump_response(WorkflowCommentResolve, result)
@console_ns.route("/apps/<uuid:app_id>/workflow/comments/<string:comment_id>/replies")
class WorkflowCommentReplyApi(Resource):
"""API for managing comment replies."""
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("create_workflow_comment_reply")
@console_ns.doc(description="Add a reply to a workflow comment")
@console_ns.doc(params={"app_id": "Application ID", "comment_id": "Comment ID"})
@console_ns.expect(console_ns.models[WorkflowCommentReplyPayload.__name__])
@console_ns.response(201, "Reply created successfully", console_ns.models[WorkflowCommentReplyCreate.__name__])
@console_account_admission(allowed_roles=_EDIT_ROLES)
@model_validate(WorkflowCommentReplyPayload)
def post(
self,
req_data: WorkflowCommentReplyPayload,
request_context: RequestContext,
app_id: UUID,
comment_id: str,
):
"""Add a reply to a workflow comment."""
with _translate_errors():
result = application_services().workflow_comments.create_reply(
request_context,
str(app_id),
comment_id,
WorkflowCommentReplyDraft(
content=req_data.content,
mentioned_user_ids=tuple(req_data.mentioned_user_ids),
),
)
return dump_response(WorkflowCommentReplyCreate, result), 201
@console_ns.route("/apps/<uuid:app_id>/workflow/comments/<string:comment_id>/replies/<string:reply_id>")
class WorkflowCommentReplyDetailApi(Resource):
"""API for managing individual comment replies."""
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("update_workflow_comment_reply")
@console_ns.doc(description="Update a comment reply")
@console_ns.doc(params={"app_id": "Application ID", "comment_id": "Comment ID", "reply_id": "Reply ID"})
@console_ns.expect(console_ns.models[WorkflowCommentReplyPayload.__name__])
@console_ns.response(200, "Reply updated successfully", console_ns.models[WorkflowCommentReplyUpdate.__name__])
@console_account_admission(allowed_roles=_EDIT_ROLES)
@model_validate(WorkflowCommentReplyPayload)
def put(
self,
req_data: WorkflowCommentReplyPayload,
request_context: RequestContext,
app_id: UUID,
comment_id: str,
reply_id: str,
):
"""Update a comment reply."""
with _translate_errors():
result = application_services().workflow_comments.update_reply(
request_context,
str(app_id),
comment_id,
reply_id,
WorkflowCommentReplyDraft(
content=req_data.content,
mentioned_user_ids=tuple(req_data.mentioned_user_ids),
),
)
return dump_response(WorkflowCommentReplyUpdate, result)
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("delete_workflow_comment_reply")
@console_ns.doc(description="Delete a comment reply")
@console_ns.doc(params={"app_id": "Application ID", "comment_id": "Comment ID", "reply_id": "Reply ID"})
@console_ns.response(204, "Reply deleted successfully")
@console_account_admission(allowed_roles=_EDIT_ROLES)
def delete(self, request_context: RequestContext, app_id: UUID, comment_id: str, reply_id: str):
"""Delete a comment reply."""
with _translate_errors():
application_services().workflow_comments.delete_reply(request_context, str(app_id), comment_id, reply_id)
return "", 204
@console_ns.route("/apps/<uuid:app_id>/workflow/comments/mention-users")
class WorkflowCommentMentionUsersApi(Resource):
"""API for getting mentionable users for workflow comments."""
@error_responses(AppNotFoundError, ForbiddenError)
@console_ns.doc("workflow_comment_mention_users")
@console_ns.doc(description="Get all users in current tenant for mentions")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
200, "Mentionable users retrieved successfully", console_ns.models[WorkflowCommentMentionUsersPayload.__name__]
)
@console_account_admission()
def get(self, request_context: RequestContext, app_id: UUID):
"""Get all users in current tenant for mentions."""
services = application_services()
with _translate_errors():
services.workflow_comments.ensure_app(request_context, str(app_id))
members = services.workspaces.member_queries.list_members(request_context.active_workspace_id)
return dump_response(WorkflowCommentMentionUsersPayload, {"users": members}), 200
@@ -0,0 +1,674 @@
import logging
from collections.abc import Callable
from functools import wraps
from typing import Any, Concatenate, Self
from uuid import UUID
from flask import Response
from flask_restx import Resource
from pydantic import BaseModel, ConfigDict, Field, model_validator
from sqlalchemy.orm import sessionmaker
from controllers.common.errors import InvalidArgumentError, NotFoundError
from controllers.common.fields import SimpleResultResponse
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import (
DraftWorkflowNotExist,
)
from controllers.console.app.wraps import get_app_model
from controllers.console.wraps import (
RBACPermission,
account_initialization_required,
edit_permission_required,
model_validate,
rbac_permission_required,
setup_required,
with_current_user,
)
from core.app.file_access import DatabaseFileAccessController
from core.workflow.llm_environment_variable import environment_variable_value_type
from core.workflow.variable_prefixes import CONVERSATION_VARIABLE_NODE_ID, SYSTEM_VARIABLE_NODE_ID
from extensions.ext_database import db
from factories import variable_factory
from factories.file_factory import build_from_mapping, build_from_mappings
from factories.variable_factory import build_segment_with_type
from fields.base import ResponseModel
from fields.workflow_draft_variable_fields import (
WorkflowDraftVariableFullContentResponse,
WorkflowDraftVariableListResponse,
WorkflowDraftVariableListWithoutValueResponse,
WorkflowDraftVariableResponse,
WorkflowDraftVariableWithoutValueResponse,
)
from graphon.variables.types import SegmentType
from libs.external_api import error_responses
from libs.helper import dump_response
from libs.login import login_required
from models import Account, App, AppMode
from models.workflow import WorkflowDraftVariable
from services.workflow_draft_variable_service import WorkflowDraftVariableList, WorkflowDraftVariableService
from services.workflow_service import WorkflowService
logger = logging.getLogger(__name__)
_file_access_controller = DatabaseFileAccessController()
class WorkflowDraftVariableListQuery(BaseModel):
page: int = Field(default=1, ge=1, le=100_000, description="Page number")
limit: int = Field(default=20, ge=1, le=100, description="Items per page")
class WorkflowDraftVariableUpdatePayload(BaseModel):
name: str | None = Field(default=None, description="Variable name")
value: Any | None = Field(default=None, description="Variable value")
class WorkflowVariableItemPayload(BaseModel):
model_config = ConfigDict(extra="allow")
id: str | None = None
name: str | None = None
value_type: str | None = None
value: Any | None = None
description: str | None = None
class ConversationVariableItemPayload(WorkflowVariableItemPayload):
pass
class EnvironmentVariableItemPayload(WorkflowVariableItemPayload):
pass
class ConversationVariableUpdatePayload(BaseModel):
conversation_variables: list[ConversationVariableItemPayload] = Field(
...,
description="Conversation variables for the draft workflow",
)
class EnvironmentVariableUpdatePayload(BaseModel):
environment_variables: list[EnvironmentVariableItemPayload] = Field(
...,
description="Environment variables for the draft workflow",
)
patch: bool = Field(
default=False,
description="Treat environment_variables as per-ID upserts instead of replacing the full collection",
)
deleted_environment_variable_ids: list[str] = Field(
default_factory=list,
description="Environment variable IDs to delete when patch is true",
)
@model_validator(mode="after")
def validate_patch(self) -> Self:
"""Validate the per-variable patch contract without changing legacy replacement requests."""
if not self.patch:
if self.deleted_environment_variable_ids:
raise ValueError("deleted_environment_variable_ids requires patch=true")
return self
upsert_ids = [variable.id for variable in self.environment_variables]
if any(not variable_id for variable_id in upsert_ids):
raise ValueError("patched environment variables require an id")
if len(set(upsert_ids)) != len(upsert_ids):
raise ValueError("patched environment variable ids must be unique")
if any(not variable_id for variable_id in self.deleted_environment_variable_ids):
raise ValueError("deleted environment variable ids must not be empty")
if len(set(self.deleted_environment_variable_ids)) != len(self.deleted_environment_variable_ids):
raise ValueError("deleted environment variable ids must be unique")
if set(upsert_ids).intersection(self.deleted_environment_variable_ids):
raise ValueError("an environment variable cannot be upserted and deleted in the same patch")
return self
class EnvironmentVariableItemResponse(ResponseModel):
id: str
type: str
name: str
description: str | None = None
selector: list[str]
value_type: str
value: Any
edited: bool
visible: bool
editable: bool
class EnvironmentVariableListResponse(ResponseModel):
items: list[EnvironmentVariableItemResponse]
register_schema_models(
console_ns,
WorkflowDraftVariableListQuery,
WorkflowDraftVariableUpdatePayload,
ConversationVariableItemPayload,
ConversationVariableUpdatePayload,
EnvironmentVariableItemPayload,
EnvironmentVariableUpdatePayload,
)
register_response_schema_models(console_ns, SimpleResultResponse, EnvironmentVariableListResponse)
register_response_schema_models(
console_ns,
WorkflowDraftVariableFullContentResponse,
WorkflowDraftVariableWithoutValueResponse,
WorkflowDraftVariableResponse,
WorkflowDraftVariableListWithoutValueResponse,
WorkflowDraftVariableListResponse,
)
def ensure_variable_access(
variable: WorkflowDraftVariable | None,
app_id: str,
variable_id: str,
current_user_id: str,
) -> WorkflowDraftVariable:
if variable is None:
raise NotFoundError(description=f"variable not found, id={variable_id}")
if variable.app_id != app_id or variable.user_id != current_user_id:
raise NotFoundError(description=f"variable not found, id={variable_id}")
return variable
def _api_prerequisite[T, **P, R](
f: Callable[Concatenate[T, Account, P], R],
) -> Callable[Concatenate[T, P], R | Response]:
"""Common prerequisites for all draft workflow variable APIs.
It ensures the following conditions are satisfied:
- Dify has been property setup.
- The request user has logged in and initialized.
- The requested app is a workflow or a chat flow.
- The request user has the edit permission for the app.
"""
@setup_required
@login_required
@account_initialization_required
@edit_permission_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
@with_current_user
@wraps(f)
def wrapper(self: T, current_user: Account, *args: P.args, **kwargs: P.kwargs) -> R | Response:
return f(self, current_user, *args, **kwargs)
return wrapper
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/variables")
class WorkflowVariableCollectionApi(Resource):
@error_responses(DraftWorkflowNotExist)
@console_ns.doc(params=query_params_from_model(WorkflowDraftVariableListQuery))
@console_ns.doc("get_workflow_variables")
@console_ns.doc(description="Get draft workflow variables")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params={"page": "Page number (1-100000)", "limit": "Number of items per page (1-100)"})
@console_ns.response(
200,
"Workflow variables retrieved successfully",
console_ns.models[WorkflowDraftVariableListWithoutValueResponse.__name__],
)
@_api_prerequisite
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@model_validate(WorkflowDraftVariableListQuery)
def get(self, req_data: WorkflowDraftVariableListQuery, current_user: Account, app_model: App):
"""
Get draft workflow
"""
# fetch draft workflow by app_model
workflow_service = WorkflowService()
workflow_exist = workflow_service.is_workflow_exist(app_model=app_model, session=db.session())
if not workflow_exist:
raise DraftWorkflowNotExist()
# fetch draft workflow by app_model
with sessionmaker(bind=db.engine, expire_on_commit=False).begin() as session:
draft_var_srv = WorkflowDraftVariableService(
session=session,
)
workflow_vars = draft_var_srv.list_variables_without_values(
app_id=app_model.id,
page=req_data.page,
limit=req_data.limit,
user_id=current_user.id,
)
return dump_response(WorkflowDraftVariableListWithoutValueResponse, workflow_vars)
@console_ns.doc("delete_workflow_variables")
@console_ns.doc(description="Delete all draft workflow variables")
@console_ns.response(204, "Workflow variables deleted successfully")
@_api_prerequisite
def delete(self, current_user: Account, app_model: App):
draft_var_srv = WorkflowDraftVariableService(
session=db.session(),
)
draft_var_srv.delete_user_workflow_variables(app_model.id, user_id=current_user.id)
db.session.commit()
return Response("", 204)
def validate_node_id(node_id: str) -> None:
if node_id in [
CONVERSATION_VARIABLE_NODE_ID,
SYSTEM_VARIABLE_NODE_ID,
]:
# NOTE(QuantumGhost): While we store the system and conversation variables as node variables
# with specific `node_id` in database, we still want to make the API separated. By disallowing
# accessing system and conversation variables in `WorkflowDraftNodeVariableListApi`,
# we mitigate the risk that user of the API depending on the implementation detail of the API.
#
# ref: [Hyrum's Law](https://www.hyrumslaw.com/)
raise InvalidArgumentError(
f"invalid node_id, please use correspond api for conversation and system variables, node_id={node_id}",
)
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/nodes/<string:node_id>/variables")
class NodeVariableCollectionApi(Resource):
@console_ns.doc("get_node_variables")
@console_ns.doc(description="Get variables for a specific node")
@console_ns.doc(params={"app_id": "Application ID", "node_id": "Node ID"})
@console_ns.response(
200,
"Node variables retrieved successfully",
console_ns.models[WorkflowDraftVariableListResponse.__name__],
)
@_api_prerequisite
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
def get(self, current_user: Account, app_model: App, node_id: str):
validate_node_id(node_id)
with sessionmaker(bind=db.engine, expire_on_commit=False).begin() as session:
draft_var_srv = WorkflowDraftVariableService(
session=session,
)
node_vars = draft_var_srv.list_node_variables(app_model.id, node_id, user_id=current_user.id)
return dump_response(WorkflowDraftVariableListResponse, node_vars)
@console_ns.doc("delete_node_variables")
@console_ns.doc(description="Delete all variables for a specific node")
@console_ns.response(204, "Node variables deleted successfully")
@_api_prerequisite
def delete(self, current_user: Account, app_model: App, node_id: str):
validate_node_id(node_id)
srv = WorkflowDraftVariableService(db.session())
srv.delete_node_variables(app_model.id, node_id, user_id=current_user.id)
db.session.commit()
return Response("", 204)
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/variables/<uuid:variable_id>")
class VariableApi(Resource):
_PATCH_NAME_FIELD = "name"
_PATCH_VALUE_FIELD = "value"
@console_ns.doc("get_variable")
@console_ns.doc(description="Get a specific workflow variable")
@console_ns.doc(params={"app_id": "Application ID", "variable_id": "Variable ID"})
@console_ns.response(
200,
"Variable retrieved successfully",
console_ns.models[WorkflowDraftVariableResponse.__name__],
)
@console_ns.response(404, "Variable not found")
@_api_prerequisite
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
def get(self, current_user: Account, app_model: App, variable_id: UUID):
draft_var_srv = WorkflowDraftVariableService(
session=db.session(),
)
variable_id_str = str(variable_id)
variable = ensure_variable_access(
variable=draft_var_srv.get_variable(variable_id=variable_id_str),
app_id=app_model.id,
variable_id=variable_id_str,
current_user_id=current_user.id,
)
return dump_response(WorkflowDraftVariableResponse, variable)
@console_ns.doc("update_variable")
@console_ns.doc(description="Update a workflow variable")
@console_ns.expect(console_ns.models[WorkflowDraftVariableUpdatePayload.__name__])
@console_ns.response(
200,
"Variable updated successfully",
console_ns.models[WorkflowDraftVariableResponse.__name__],
)
@console_ns.response(404, "Variable not found")
@_api_prerequisite
@model_validate(WorkflowDraftVariableUpdatePayload)
def patch(
self,
req_data: WorkflowDraftVariableUpdatePayload,
current_user: Account,
app_model: App,
variable_id: UUID,
):
# Request payload for file types:
#
# Local File:
#
# {
# "type": "image",
# "transfer_method": "local_file",
# "url": "",
# "upload_file_id": "daded54f-72c7-4f8e-9d18-9b0abdd9f190"
# }
#
# Remote File:
#
#
# {
# "type": "image",
# "transfer_method": "remote_url",
# "url": "http://127.0.0.1:5001/files/1602650a-4fe4-423c-85a2-af76c083e3c4/file-preview?timestamp=1750041099&nonce=...&sign=...=",
# "upload_file_id": "1602650a-4fe4-423c-85a2-af76c083e3c4"
# }
draft_var_srv = WorkflowDraftVariableService(
session=db.session(),
)
variable_id_str = str(variable_id)
variable = ensure_variable_access(
variable=draft_var_srv.get_variable(variable_id=variable_id_str),
app_id=app_model.id,
variable_id=variable_id_str,
current_user_id=current_user.id,
)
new_name = req_data.name
raw_value = req_data.value
if new_name is None and raw_value is None:
return dump_response(WorkflowDraftVariableResponse, variable)
new_value = None
if raw_value is not None:
match variable.value_type:
case SegmentType.FILE:
if not isinstance(raw_value, dict):
raise InvalidArgumentError(description=f"expected dict for file, got {type(raw_value)}")
raw_value = build_from_mapping(
mapping=raw_value,
tenant_id=app_model.tenant_id,
access_controller=_file_access_controller,
)
case SegmentType.ARRAY_FILE:
if not isinstance(raw_value, list):
raise InvalidArgumentError(description=f"expected list for files, got {type(raw_value)}")
if len(raw_value) > 0 and not isinstance(raw_value[0], dict):
raise InvalidArgumentError(description=f"expected dict for files[0], got {type(raw_value)}")
raw_value = build_from_mappings(
mappings=raw_value,
tenant_id=app_model.tenant_id,
access_controller=_file_access_controller,
)
case _:
pass
new_value = build_segment_with_type(variable.value_type, raw_value)
draft_var_srv.update_variable(variable, name=new_name, value=new_value)
db.session.commit()
return dump_response(WorkflowDraftVariableResponse, variable)
@console_ns.doc("delete_variable")
@console_ns.doc(description="Delete a workflow variable")
@console_ns.response(204, "Variable deleted successfully")
@console_ns.response(404, "Variable not found")
@_api_prerequisite
def delete(self, current_user: Account, app_model: App, variable_id: UUID):
draft_var_srv = WorkflowDraftVariableService(
session=db.session(),
)
variable_id_str = str(variable_id)
variable = ensure_variable_access(
variable=draft_var_srv.get_variable(variable_id=variable_id_str),
app_id=app_model.id,
variable_id=variable_id_str,
current_user_id=current_user.id,
)
draft_var_srv.delete_variable(variable)
db.session.commit()
return Response("", 204)
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/variables/<uuid:variable_id>/reset")
class VariableResetApi(Resource):
@console_ns.doc("reset_variable")
@console_ns.doc(description="Reset a workflow variable to its default value")
@console_ns.doc(params={"app_id": "Application ID", "variable_id": "Variable ID"})
@console_ns.response(
200,
"Variable reset successfully",
console_ns.models[WorkflowDraftVariableResponse.__name__],
)
@console_ns.response(204, "Variable reset (no content)")
@console_ns.response(404, "Variable not found")
@_api_prerequisite
def put(self, current_user: Account, app_model: App, variable_id: UUID):
draft_var_srv = WorkflowDraftVariableService(
session=db.session(),
)
workflow_srv = WorkflowService()
draft_workflow = workflow_srv.get_draft_workflow(app_model, session=db.session())
if draft_workflow is None:
raise NotFoundError(
f"Draft workflow not found, app_id={app_model.id}",
)
variable_id_str = str(variable_id)
variable = ensure_variable_access(
variable=draft_var_srv.get_variable(variable_id=variable_id_str),
app_id=app_model.id,
variable_id=variable_id_str,
current_user_id=current_user.id,
)
resetted = draft_var_srv.reset_variable(draft_workflow, variable)
db.session.commit()
if resetted is None:
return Response("", 204)
return dump_response(WorkflowDraftVariableResponse, resetted)
def _get_variable_list(app_model: App, node_id: str, current_user_id: str) -> WorkflowDraftVariableList:
with sessionmaker(bind=db.engine, expire_on_commit=False).begin() as session:
draft_var_srv = WorkflowDraftVariableService(
session=session,
)
if node_id == CONVERSATION_VARIABLE_NODE_ID:
draft_vars = draft_var_srv.list_conversation_variables(app_model.id, user_id=current_user_id)
elif node_id == SYSTEM_VARIABLE_NODE_ID:
draft_vars = draft_var_srv.list_system_variables(app_model.id, user_id=current_user_id)
else:
draft_vars = draft_var_srv.list_node_variables(
app_id=app_model.id,
node_id=node_id,
user_id=current_user_id,
)
return draft_vars
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/conversation-variables")
class ConversationVariableCollectionApi(Resource):
@console_ns.doc("get_conversation_variables")
@console_ns.doc(description="Get conversation variables for workflow")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
200,
"Conversation variables retrieved successfully",
console_ns.models[WorkflowDraftVariableListResponse.__name__],
)
@console_ns.response(404, "Draft workflow not found")
@_api_prerequisite
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
def get(self, current_user: Account, app_model: App):
# NOTE(QuantumGhost): Prefill conversation variables into the draft variables table
# so their IDs can be returned to the caller.
workflow_srv = WorkflowService()
draft_workflow = workflow_srv.get_draft_workflow(app_model, session=db.session())
if draft_workflow is None:
raise NotFoundError(description=f"draft workflow not found, id={app_model.id}")
draft_var_srv = WorkflowDraftVariableService(db.session())
draft_var_srv.prefill_conversation_variable_default_values(draft_workflow, user_id=current_user.id)
db.session.commit()
return dump_response(
WorkflowDraftVariableListResponse,
_get_variable_list(app_model, CONVERSATION_VARIABLE_NODE_ID, current_user.id),
)
@console_ns.expect(console_ns.models[ConversationVariableUpdatePayload.__name__])
@console_ns.doc("update_conversation_variables")
@console_ns.doc(description="Update conversation variables for workflow draft")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
200,
"Conversation variables updated successfully",
console_ns.models[SimpleResultResponse.__name__],
)
@setup_required
@login_required
@account_initialization_required
@edit_permission_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_EDIT, PlainApp()))
@with_current_user
@get_app_model(mode=AppMode.ADVANCED_CHAT)
@model_validate(ConversationVariableUpdatePayload)
def post(self, req_data: ConversationVariableUpdatePayload, current_user: Account, app_model: App):
workflow_service = WorkflowService()
conversation_variables_list = [
variable.model_dump(mode="json", exclude_unset=True) for variable in req_data.conversation_variables
]
conversation_variables = [
variable_factory.build_conversation_variable_from_mapping(obj) for obj in conversation_variables_list
]
workflow_service.update_draft_workflow_conversation_variables(
app_model=app_model,
account=current_user,
conversation_variables=conversation_variables,
session=db.session(),
)
return {"result": "success"}
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/system-variables")
class SystemVariableCollectionApi(Resource):
@console_ns.doc("get_system_variables")
@console_ns.doc(description="Get system variables for workflow")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
200,
"System variables retrieved successfully",
console_ns.models[WorkflowDraftVariableListResponse.__name__],
)
@_api_prerequisite
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
def get(self, current_user: Account, app_model: App):
return dump_response(
WorkflowDraftVariableListResponse,
_get_variable_list(app_model, SYSTEM_VARIABLE_NODE_ID, current_user.id),
)
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/environment-variables")
class EnvironmentVariableCollectionApi(Resource):
@error_responses(DraftWorkflowNotExist)
@console_ns.doc("get_environment_variables")
@console_ns.doc(description="Get environment variables for workflow")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
200,
"Environment variables retrieved successfully",
console_ns.models[EnvironmentVariableListResponse.__name__],
)
@console_ns.response(404, "Draft workflow not found")
@_api_prerequisite
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
def get(self, _current_user: Account, app_model: App):
"""
Get draft workflow
"""
# fetch draft workflow by app_model
workflow_service = WorkflowService()
workflow = workflow_service.get_draft_workflow(app_model=app_model, session=db.session())
if workflow is None:
raise DraftWorkflowNotExist()
env_vars = workflow.environment_variables
env_vars_list = []
for v in env_vars:
env_vars_list.append(
{
"id": v.id,
"type": "env",
"name": v.name,
"description": v.description,
"selector": v.selector,
"value_type": environment_variable_value_type(v),
"value": v.value,
# Do not track edited for env vars.
"edited": False,
"visible": True,
"editable": True,
}
)
return {"items": env_vars_list}
@console_ns.expect(console_ns.models[EnvironmentVariableUpdatePayload.__name__])
@console_ns.doc("update_environment_variables")
@console_ns.doc(description="Update environment variables for workflow draft")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.response(
200,
"Environment variables updated successfully",
console_ns.models[SimpleResultResponse.__name__],
)
@setup_required
@login_required
@account_initialization_required
@edit_permission_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_EDIT, PlainApp()))
@with_current_user
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
@model_validate(EnvironmentVariableUpdatePayload)
def post(self, req_data: EnvironmentVariableUpdatePayload, current_user: Account, app_model: App):
workflow_service = WorkflowService()
environment_variables_list = [
variable.model_dump(mode="json", exclude_unset=True) for variable in req_data.environment_variables
]
environment_variables = [
variable_factory.build_environment_variable_from_mapping(obj) for obj in environment_variables_list
]
if req_data.patch:
workflow_service.patch_draft_workflow_environment_variables(
app_model=app_model,
account=current_user,
environment_variables=environment_variables,
deleted_environment_variable_ids=req_data.deleted_environment_variable_ids,
session=db.session(),
)
else:
workflow_service.update_draft_workflow_environment_variables(
app_model=app_model,
account=current_user,
environment_variables=environment_variables,
session=db.session(),
)
return {"result": "success"}
@@ -0,0 +1,90 @@
"""Draft Human Input v2 message-template tests for the authenticated editor."""
from __future__ import annotations
from http import HTTPStatus
from flask_restx import Resource
from controllers.common.human_input_v2_contracts import (
MessageTemplateTestRequest,
MessageTemplateTestResponse,
)
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.wraps import (
RBACPermission,
account_initialization_required,
edit_permission_required,
rbac_permission_required,
setup_required,
with_current_user,
)
from libs.exception import BaseHTTPException
from libs.login import login_required
from models import Account
from models.model import App, AppMode
from services.human_input_v2.message_template_test_composition import build_message_template_test_service
from services.human_input_v2.message_template_test_service import MessageTemplateSendError, MessageTemplateTestError
from .wraps import get_app_model
register_schema_models(console_ns, MessageTemplateTestRequest)
register_response_schema_models(console_ns, MessageTemplateTestResponse)
class MessageTemplateTestHttpError(BaseHTTPException):
code = HTTPStatus.BAD_REQUEST
error_code = "message_template_test_invalid"
class MessageTemplateSendHttpError(BaseHTTPException):
code = HTTPStatus.BAD_GATEWAY
error_code = "message_template_test_send_failed"
def _send_test(current_user: Account, app_model: App, node_id: str) -> dict[str, object]:
payload = MessageTemplateTestRequest.model_validate(console_ns.payload or {})
service = build_message_template_test_service(
tenant_id=app_model.tenant_id, account_id=current_user.id, account_email=current_user.email
)
try:
service.send_test(app_id=app_model.id, node_id=node_id, channel=payload.channel, inputs=payload.inputs)
except MessageTemplateSendError as error:
raise MessageTemplateSendHttpError(str(error)) from None
except MessageTemplateTestError as error:
raise MessageTemplateTestHttpError(str(error)) from None
return MessageTemplateTestResponse().model_dump(mode="json")
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/human-input/nodes/<string:node_id>/message-template/test")
class WorkflowDraftMessageTemplateTestApi(Resource):
@console_ns.expect(console_ns.models[MessageTemplateTestRequest.__name__])
@console_ns.response(200, "Success", console_ns.models[MessageTemplateTestResponse.__name__])
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_TEST_AND_RUN, PlainApp()))
@get_app_model(mode=[AppMode.WORKFLOW])
@with_current_user
@edit_permission_required
def post(self, current_user: Account, app_model: App, node_id: str):
return _send_test(current_user, app_model, node_id)
@console_ns.route(
"/apps/<uuid:app_id>/advanced-chat/workflows/draft/human-input/nodes/<string:node_id>/message-template/test"
)
class AdvancedChatDraftMessageTemplateTestApi(Resource):
@console_ns.expect(console_ns.models[MessageTemplateTestRequest.__name__])
@console_ns.response(200, "Success", console_ns.models[MessageTemplateTestResponse.__name__])
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_TEST_AND_RUN, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT])
@with_current_user
@edit_permission_required
def post(self, current_user: Account, app_model: App, node_id: str):
return _send_test(current_user, app_model, node_id)
@@ -0,0 +1,510 @@
"""Console REST endpoints for the Node Output Inspector (Stage 4 §8 / §10.3).
PRD §Node Output Inspector replaces the consumer-organized Variable Inspector
with a producer-organized view of each node's declared outputs and their
per-run status. This module exposes two parallel sets of three read-only
endpoints — one for ``/workflows/draft/runs/...`` (Composer test runs) and one
for ``/workflows/published/runs/...`` (real App API / webapp / webhook /
schedule / plugin triggers). Both sets share the same service code, the same
response shapes, and the same error codes; the URL is the *only* difference,
so the frontend can pick the right prefix based on which run-detail page the
user is on.
Decision D-1 (published Inspector deferred) was lifted 2026-05-26 — the
``published_run_inspector_not_implemented`` 404 code is therefore no longer
produced.
URLs follow the design doc and reuse the existing
``/apps/<uuid:app_id>/workflows/draft/...`` prefix from
:mod:`controllers.console.app.workflow_draft_variable`. The
``published`` prefix mirrors it shape-for-shape.
"""
from __future__ import annotations
import json
import logging
from collections.abc import Iterator
from uuid import UUID
from flask import Response
from flask_restx import Resource
from controllers.common.fields import EventStreamResponse
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import register_response_schema_models
from controllers.console import console_ns
from controllers.console.app.wraps import get_app_model
from controllers.console.wraps import (
RBACPermission,
account_initialization_required,
rbac_permission_required,
setup_required,
)
from extensions.ext_database import db
from libs.exception import BaseHTTPException, RelayedHTTPError
from libs.external_api import error_responses
from libs.login import login_required
from models import App, AppMode
from services.workflow import inspector_events
from services.workflow.node_output_inspector_service import (
CheckResultView,
NodeOutputInspectorError,
NodeOutputInspectorService,
NodeOutputsView,
NodeOutputView,
OutputPreviewView,
WorkflowRunSnapshotView,
)
logger = logging.getLogger(__name__)
# Heartbeat cadence — every N empty subscribe ticks emit a SSE comment so
# intervening proxies (nginx, ingress) don't reap the idle connection.
# ``inspector_events.subscribe`` ticks at 1s, so 15 → 15s heartbeat.
_HEARTBEAT_EVERY_TICKS = 15
# Hard ceiling on a single stream — if we never see a terminal workflow
# event (engine crashed, redis dropped the message), force-close after this
# many ticks (= seconds).
_STREAM_HARD_TIMEOUT_TICKS = 1800 # 30 min
register_response_schema_models(
console_ns,
EventStreamResponse,
CheckResultView,
NodeOutputView,
NodeOutputsView,
WorkflowRunSnapshotView,
OutputPreviewView,
)
def _service() -> NodeOutputInspectorService:
"""One-line factory so tests can monkeypatch a stub if needed."""
return NodeOutputInspectorService()
def _serve_snapshot(app_model: App, run_id: UUID) -> dict:
"""Resource-body shared by draft + published snapshot endpoints.
Pulled out so the 6 REST routes don't duplicate the same 6-line try/except
+ ``model_dump`` ritual — the routes shrink to one-liners and the actual
behaviour lives here, where unit tests can hit it without spinning up
Flask request context.
"""
try:
snapshot = _service().snapshot_workflow_run(
app_model=app_model, workflow_run_id=str(run_id), session=db.session()
)
except NodeOutputInspectorError as error:
raise _inspector_not_found(error) from error
return snapshot.model_dump(mode="json")
def _serve_node_detail(app_model: App, run_id: UUID, node_id: str) -> dict:
"""Resource-body shared by draft + published node-detail endpoints."""
try:
view = _service().node_detail(
app_model=app_model,
workflow_run_id=str(run_id),
node_id=node_id,
session=db.session(),
)
except NodeOutputInspectorError as error:
raise _inspector_not_found(error) from error
return view.model_dump(mode="json")
def _serve_output_preview(app_model: App, run_id: UUID, node_id: str, output_name: str) -> dict:
"""Resource-body shared by draft + published output-preview endpoints."""
try:
preview = _service().output_preview(
app_model=app_model,
workflow_run_id=str(run_id),
node_id=node_id,
output_name=output_name,
session=db.session(),
)
except NodeOutputInspectorError as error:
raise _inspector_not_found(error) from error
return preview.model_dump(mode="json")
class _InspectorNotFound(BaseHTTPException):
"""404 that keeps the inspector's own error code and message.
Without this the response body collapses to a generic ``not_found`` code and
clients cannot tell ``workflow_run_not_found`` from ``node_not_in_workflow_run``.
Each code the service raises is a subclass, so the contract documents it.
"""
code = 404
def __init__(self, error: NodeOutputInspectorError) -> None:
super().__init__(description=str(error))
class WorkflowRunNotFoundError(_InspectorNotFound):
error_code = "workflow_run_not_found"
description = "Workflow run not found."
class NodeNotInWorkflowRunError(_InspectorNotFound):
error_code = "node_not_in_workflow_run"
description = "The node is not part of this workflow run."
class NodeOutputNotDeclaredError(_InspectorNotFound):
error_code = "node_output_not_declared"
description = "The node does not declare this output."
def _inspector_not_found(error: NodeOutputInspectorError) -> BaseHTTPException:
"""The 404 for an inspector error; a code the service adds later is relayed as is."""
if error.code == WorkflowRunNotFoundError.error_code:
return WorkflowRunNotFoundError(error)
if error.code == NodeNotInWorkflowRunError.error_code:
return NodeNotInWorkflowRunError(error)
if error.code == NodeOutputNotDeclaredError.error_code:
return NodeOutputNotDeclaredError(error)
return RelayedHTTPError(status=404, error_code=error.code, message=str(error))
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/runs/<uuid:run_id>/node-outputs")
class WorkflowDraftRunNodeOutputsApi(Resource):
"""Whole-run snapshot organized by producer node."""
@error_responses(RelayedHTTPError, WorkflowRunNotFoundError)
@console_ns.doc("get_workflow_draft_run_node_outputs")
@console_ns.doc(description="Snapshot of every node's declared outputs for a draft workflow run.")
@console_ns.doc(params={"app_id": "Application ID", "run_id": "Workflow run ID"})
@console_ns.response(200, "Workflow run node outputs", console_ns.models[WorkflowRunSnapshotView.__name__])
@console_ns.response(404, "Workflow run not found")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, app_model: App, run_id: UUID):
return _serve_snapshot(app_model, run_id)
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/runs/<uuid:run_id>/node-outputs/<string:node_id>")
class WorkflowDraftRunNodeOutputDetailApi(Resource):
"""One node's declared outputs + per-output status."""
@error_responses(NodeNotInWorkflowRunError, RelayedHTTPError, WorkflowRunNotFoundError)
@console_ns.doc("get_workflow_draft_run_node_output_detail")
@console_ns.doc(description="One node's declared outputs for a draft workflow run.")
@console_ns.doc(
params={
"app_id": "Application ID",
"run_id": "Workflow run ID",
"node_id": "Node ID inside the workflow graph",
}
)
@console_ns.response(200, "Workflow run node output detail", console_ns.models[NodeOutputsView.__name__])
@console_ns.response(404, "Workflow run / node not found")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, app_model: App, run_id: UUID, node_id: str):
return _serve_node_detail(app_model, run_id, node_id)
@console_ns.route(
"/apps/<uuid:app_id>/workflows/draft/runs/<uuid:run_id>/node-outputs/<string:node_id>/<string:output_name>/preview"
)
class WorkflowDraftRunNodeOutputPreviewApi(Resource):
"""Full value for one declared output (with signed URL for file refs)."""
@error_responses(NodeNotInWorkflowRunError, NodeOutputNotDeclaredError, RelayedHTTPError, WorkflowRunNotFoundError)
@console_ns.doc("get_workflow_draft_run_node_output_preview")
@console_ns.doc(description="Full value for one declared output, including signed download URL for files.")
@console_ns.doc(
params={
"app_id": "Application ID",
"run_id": "Workflow run ID",
"node_id": "Node ID inside the workflow graph",
"output_name": "Declared output name as exposed by Composer",
}
)
@console_ns.response(200, "Workflow run node output preview", console_ns.models[OutputPreviewView.__name__])
@console_ns.response(404, "Workflow run / node / output not found")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, app_model: App, run_id: UUID, node_id: str, output_name: str):
return _serve_output_preview(app_model, run_id, node_id, output_name)
# ──────────────────────────────────────────────────────────────────────────────
# SSE event stream — shared generator used by draft + published variants
# ──────────────────────────────────────────────────────────────────────────────
def _sse_envelope(event: str, data: dict | str, event_id: int) -> str:
"""Format one SSE record per D-5 ``{event, data, id}`` envelope.
``data`` is JSON-serialized when given as a dict; raw strings are
forwarded unchanged so we can also emit ``:keepalive`` comment lines.
"""
payload = data if isinstance(data, str) else json.dumps(data, ensure_ascii=False)
return f"event: {event}\nid: {event_id}\ndata: {payload}\n\n"
def _initial_snapshot(app_model: App, run_id: UUID) -> WorkflowRunSnapshotView:
"""The snapshot an event stream opens with, or the run's 404.
Called before the streaming ``Response`` is built: once a generator is handed to
``Response`` the 200 status has already gone out, so an error raised inside it cannot
become an HTTP error. Resolving the run here keeps a missing run a plain 404.
"""
try:
return _service().snapshot_workflow_run(app_model=app_model, workflow_run_id=str(run_id), session=db.session())
except NodeOutputInspectorError as error:
raise _inspector_not_found(error) from error
def _stream_inspector_events(app_model: App, run_id: UUID, snapshot: WorkflowRunSnapshotView) -> Iterator[str]:
"""Yield SSE-framed strings for one workflow run.
The stream begins with the full ``snapshot`` event (resolved by ``_initial_snapshot``
before the response started) so the client has a starting state without needing a
separate REST GET. Then for every
``node_changed`` message from the pub/sub channel we re-read that node
from DB and push a fresh ``node_changed`` event. When the workflow run
reaches a terminal state we push one final ``workflow_run_completed``
event and close the stream.
Failures inside the loop are caught and surfaced as ``error`` events so
the frontend can show a banner rather than seeing the connection drop
silently. The Inspector never raises across the SSE boundary.
"""
service = _service()
run_id_str = str(run_id)
event_id = 0
yield _sse_envelope("snapshot", snapshot.model_dump(mode="json"), event_id)
# If the run already finished by the time the client connected, emit
# the terminal envelope synchronously and close — no point subscribing.
# The enum value for partial success is the hyphenated ``partial-succeeded``
# (graphon.enums.WorkflowExecutionStatus), not ``partial_succeeded``.
if snapshot.workflow_run_status.value in {"succeeded", "failed", "stopped", "partial-succeeded"}:
event_id += 1
yield _sse_envelope(
"workflow_run_completed",
{"workflow_run_id": run_id_str, "workflow_run_status": snapshot.workflow_run_status.value},
event_id,
)
return
# Live subscription
ticks_since_heartbeat = 0
total_ticks = 0
for message in inspector_events.subscribe(run_id_str, timeout_seconds=1.0):
total_ticks += 1
if total_ticks > _STREAM_HARD_TIMEOUT_TICKS:
logger.warning(
"Inspector SSE: forcing close after %ds without terminal event for run %s",
_STREAM_HARD_TIMEOUT_TICKS,
run_id_str,
)
return
# Heartbeat sentinel — ``inspector_events.subscribe`` synthesizes a
# ``node_changed`` message with both fields ``None`` on every redis
# timeout. Real ``workflow_completed`` messages keep their kind even
# when status couldn't be resolved (publisher race), so checking kind
# first makes the heartbeat branch safe.
if message.kind == "node_changed" and message.node_id is None and message.status is None:
ticks_since_heartbeat += 1
if ticks_since_heartbeat >= _HEARTBEAT_EVERY_TICKS:
yield ":keepalive\n\n"
ticks_since_heartbeat = 0
continue
ticks_since_heartbeat = 0
if message.kind == "workflow_completed":
event_id += 1
yield _sse_envelope(
"workflow_run_completed",
{"workflow_run_id": run_id_str, "workflow_run_status": message.status or "unknown"},
event_id,
)
return
# node_changed: recompute the node slice from DB
if not message.node_id:
continue
try:
node_view = service.node_detail(
app_model=app_model,
workflow_run_id=run_id_str,
node_id=message.node_id,
session=db.session(),
)
except NodeOutputInspectorError:
# Node may not appear in the graph yet (race with persistence); skip.
continue
except Exception:
logger.warning(
"Inspector SSE: node_detail failed for run %s node %s",
run_id_str,
message.node_id,
exc_info=True,
)
event_id += 1
yield _sse_envelope(
"error",
{"node_id": message.node_id, "message": "failed to refresh node detail"},
event_id,
)
continue
event_id += 1
yield _sse_envelope("node_changed", node_view.model_dump(mode="json"), event_id)
@console_ns.route("/apps/<uuid:app_id>/workflows/draft/runs/<uuid:run_id>/node-outputs/events")
class WorkflowDraftRunNodeOutputEventsApi(Resource):
"""SSE stream of inspector deltas for a draft run."""
@error_responses(RelayedHTTPError, WorkflowRunNotFoundError)
@console_ns.doc("stream_workflow_draft_run_node_output_events")
@console_ns.doc(description="Server-Sent Events stream of inspector deltas for a draft workflow run.")
@console_ns.doc(params={"app_id": "Application ID", "run_id": "Workflow run ID"})
@console_ns.response(
200,
"Workflow run node output event stream",
console_ns.models[EventStreamResponse.__name__],
)
@console_ns.response(404, "Workflow run not found")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, app_model: App, run_id: UUID):
snapshot = _initial_snapshot(app_model, run_id)
return Response(
_stream_inspector_events(app_model, run_id, snapshot),
mimetype="text/event-stream",
headers={"Cache-Control": "no-cache", "Connection": "keep-alive"},
)
# ──────────────────────────────────────────────────────────────────────────────
# Published-run endpoints — symmetric to the draft trio above
# ──────────────────────────────────────────────────────────────────────────────
@console_ns.route("/apps/<uuid:app_id>/workflows/published/runs/<uuid:run_id>/node-outputs")
class WorkflowPublishedRunNodeOutputsApi(Resource):
"""Whole-run snapshot for a *published* workflow run.
Same response shape as the ``/draft/`` variant — frontend can multiplex
based on which page (Composer test-run vs. Run History) is mounted.
"""
@error_responses(RelayedHTTPError, WorkflowRunNotFoundError)
@console_ns.doc("get_workflow_published_run_node_outputs")
@console_ns.doc(description="Snapshot of every node's declared outputs for a published workflow run.")
@console_ns.doc(params={"app_id": "Application ID", "run_id": "Workflow run ID"})
@console_ns.response(200, "Workflow run node outputs", console_ns.models[WorkflowRunSnapshotView.__name__])
@console_ns.response(404, "Workflow run not found")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, app_model: App, run_id: UUID):
return _serve_snapshot(app_model, run_id)
@console_ns.route("/apps/<uuid:app_id>/workflows/published/runs/<uuid:run_id>/node-outputs/<string:node_id>")
class WorkflowPublishedRunNodeOutputDetailApi(Resource):
"""One node's declared outputs + per-output status (published run)."""
@error_responses(NodeNotInWorkflowRunError, RelayedHTTPError, WorkflowRunNotFoundError)
@console_ns.doc("get_workflow_published_run_node_output_detail")
@console_ns.doc(description="One node's declared outputs for a published workflow run.")
@console_ns.doc(
params={
"app_id": "Application ID",
"run_id": "Workflow run ID",
"node_id": "Node ID inside the workflow graph",
}
)
@console_ns.response(200, "Workflow run node output detail", console_ns.models[NodeOutputsView.__name__])
@console_ns.response(404, "Workflow run / node not found")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, app_model: App, run_id: UUID, node_id: str):
return _serve_node_detail(app_model, run_id, node_id)
@console_ns.route(
"/apps/<uuid:app_id>/workflows/published/runs/<uuid:run_id>"
"/node-outputs/<string:node_id>/<string:output_name>/preview"
)
class WorkflowPublishedRunNodeOutputPreviewApi(Resource):
"""Full value for one declared output of a published run."""
@error_responses(NodeNotInWorkflowRunError, NodeOutputNotDeclaredError, RelayedHTTPError, WorkflowRunNotFoundError)
@console_ns.doc("get_workflow_published_run_node_output_preview")
@console_ns.doc(description="Full value for one declared output of a published run.")
@console_ns.doc(
params={
"app_id": "Application ID",
"run_id": "Workflow run ID",
"node_id": "Node ID inside the workflow graph",
"output_name": "Declared output name as exposed by Composer",
}
)
@console_ns.response(200, "Workflow run node output preview", console_ns.models[OutputPreviewView.__name__])
@console_ns.response(404, "Workflow run / node / output not found")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, app_model: App, run_id: UUID, node_id: str, output_name: str):
return _serve_output_preview(app_model, run_id, node_id, output_name)
@console_ns.route("/apps/<uuid:app_id>/workflows/published/runs/<uuid:run_id>/node-outputs/events")
class WorkflowPublishedRunNodeOutputEventsApi(Resource):
"""SSE stream of inspector deltas for a published run."""
@error_responses(RelayedHTTPError, WorkflowRunNotFoundError)
@console_ns.doc("stream_workflow_published_run_node_output_events")
@console_ns.doc(description="Server-Sent Events stream of inspector deltas for a published workflow run.")
@console_ns.doc(params={"app_id": "Application ID", "run_id": "Workflow run ID"})
@console_ns.response(
200,
"Workflow run node output event stream",
console_ns.models[EventStreamResponse.__name__],
)
@console_ns.response(404, "Workflow run not found")
@setup_required
@login_required
@account_initialization_required
@rbac_permission_required(RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp()))
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, app_model: App, run_id: UUID):
snapshot = _initial_snapshot(app_model, run_id)
return Response(
_stream_inspector_events(app_model, run_id, snapshot),
mimetype="text/event-stream",
headers={"Cache-Control": "no-cache", "Connection": "keep-alive"},
)
@@ -0,0 +1,421 @@
from typing import Literal
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from sqlalchemy.orm import Session
from werkzeug.exceptions import Forbidden
from configs import dify_config
from controllers.common.errors import NotFoundError
from controllers.common.rbac import PlainApp, RBACCheck, enforce_rbac_checks
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.common.session import with_session
from controllers.console import console_ns
from controllers.console.app.wraps import get_app_model
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import (
RBACPermission,
model_validate,
)
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from fields.workflow_run_fields import (
AdvancedChatWorkflowRunPaginationResponse,
WorkflowRunCountResponse,
WorkflowRunDetailResponse,
WorkflowRunNodeExecutionListResponse,
WorkflowRunNodeExecutionResponse,
WorkflowRunPaginationResponse,
WorkflowToolNodeExecutionListResponse,
workflow_run_pagination_response_source,
workflow_run_response_source,
)
from libs.custom_inputs import time_duration
from libs.helper import dump_response, uuid_value
from machinery.context import RequestContext
from models import App, AppMode, WorkflowRunTriggeredFrom
from services.workflow_run_service import WorkflowRunListArgs
def _build_backstage_input_url(form_token: str | None) -> str | None:
if not form_token:
return None
base_url = dify_config.APP_WEB_URL
if not base_url:
return None
return f"{base_url.rstrip('/')}/form/{form_token}"
class WorkflowRunListQuery(BaseModel):
last_id: str | None = Field(default=None, description="Last run ID for pagination")
limit: int = Field(default=20, ge=1, le=100, description="Number of items per page (1-100)")
status: Literal["running", "succeeded", "failed", "stopped", "partial-succeeded"] | None = Field(
default=None, description="Workflow run status filter"
)
triggered_from: Literal["debugging", "app-run"] | None = Field(
default=None, description="Filter by trigger source: debugging or app-run. Default: debugging"
)
@field_validator("last_id")
@classmethod
def validate_last_id(cls, value: str | None) -> str | None:
if value is None:
return value
return uuid_value(value)
class WorkflowRunCountQuery(BaseModel):
status: Literal["running", "succeeded", "failed", "stopped", "partial-succeeded"] | None = Field(
default=None, description="Workflow run status filter"
)
time_range: str | None = Field(
default=None,
description=(
"Filter by time range (optional): e.g., 7d (7 days), 4h (4 hours), "
"30m (30 minutes), 30s (30 seconds). Filters by created_at field."
),
)
triggered_from: Literal["debugging", "app-run"] | None = Field(
default=None, description="Filter by trigger source: debugging or app-run. Default: debugging"
)
@field_validator("time_range")
@classmethod
def validate_time_range(cls, value: str | None) -> str | None:
if value is None:
return value
return time_duration(value)
def _workflow_run_list_args(req_data: WorkflowRunListQuery) -> WorkflowRunListArgs:
args: WorkflowRunListArgs = {"limit": req_data.limit}
if req_data.last_id is not None:
args["last_id"] = req_data.last_id
if req_data.status is not None:
args["status"] = req_data.status
return args
def _triggered_from(value: str | None) -> WorkflowRunTriggeredFrom:
return WorkflowRunTriggeredFrom(value) if value else WorkflowRunTriggeredFrom.DEBUGGING
class HumanInputPauseTypeResponse(ResponseModel):
type: Literal["human_input"]
form_id: str
form_version: Literal["1", "2"] = "1"
backstage_input_url: str | None = None
class PausedNodeResponse(ResponseModel):
node_id: str
node_title: str
pause_type: HumanInputPauseTypeResponse
class WorkflowPauseDetailsResponse(ResponseModel):
paused_at: str | None = None
paused_nodes: list[PausedNodeResponse]
register_schema_models(
console_ns,
WorkflowRunListQuery,
WorkflowRunCountQuery,
)
register_response_schema_models(
console_ns,
AdvancedChatWorkflowRunPaginationResponse,
WorkflowRunPaginationResponse,
WorkflowRunCountResponse,
WorkflowRunDetailResponse,
WorkflowToolNodeExecutionListResponse,
WorkflowRunNodeExecutionResponse,
WorkflowRunNodeExecutionListResponse,
HumanInputPauseTypeResponse,
PausedNodeResponse,
WorkflowPauseDetailsResponse,
)
@console_ns.route("/apps/<uuid:app_id>/advanced-chat/workflow-runs")
class AdvancedChatAppWorkflowRunListApi(Resource):
@console_ns.doc("get_advanced_chat_workflow_runs")
@console_ns.doc(description="Get advanced chat workflow run list")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(WorkflowRunListQuery))
@console_ns.response(
200,
"Workflow runs retrieved successfully",
console_ns.models[AdvancedChatWorkflowRunPaginationResponse.__name__],
)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())],
)
@get_app_model(mode=[AppMode.ADVANCED_CHAT])
@model_validate(WorkflowRunListQuery)
@with_session(write=False)
def get(self, session: Session, req_data: WorkflowRunListQuery, request_context: RequestContext, app_model: App):
"""
Get advanced chat app workflow run list
"""
result = application_services().workflow_runs.get_paginate_advanced_chat_workflow_runs(
request_context,
app_id=app_model.id,
args=_workflow_run_list_args(req_data),
triggered_from=_triggered_from(req_data.triggered_from),
)
return dump_response(
AdvancedChatWorkflowRunPaginationResponse,
workflow_run_pagination_response_source(result, session=session),
)
@console_ns.route("/apps/<uuid:app_id>/advanced-chat/workflow-runs/count")
class AdvancedChatAppWorkflowRunCountApi(Resource):
@console_ns.doc("get_advanced_chat_workflow_runs_count")
@console_ns.doc(description="Get advanced chat workflow runs count statistics")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(WorkflowRunCountQuery))
@console_ns.response(
200,
"Workflow runs count retrieved successfully",
console_ns.models[WorkflowRunCountResponse.__name__],
)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())],
)
@get_app_model(mode=[AppMode.ADVANCED_CHAT])
@model_validate(WorkflowRunCountQuery)
def get(self, req_data: WorkflowRunCountQuery, request_context: RequestContext, app_model: App):
"""
Get advanced chat workflow runs count statistics
"""
result = application_services().workflow_runs.get_workflow_runs_count(
request_context,
app_id=app_model.id,
status=req_data.status,
time_range=req_data.time_range,
triggered_from=_triggered_from(req_data.triggered_from),
)
return dump_response(WorkflowRunCountResponse, result)
@console_ns.route("/apps/<uuid:app_id>/workflow-runs")
class WorkflowRunListApi(Resource):
@console_ns.doc("get_workflow_runs")
@console_ns.doc(description="Get workflow run list")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(WorkflowRunListQuery))
@console_ns.response(
200,
"Workflow runs retrieved successfully",
console_ns.models[WorkflowRunPaginationResponse.__name__],
)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())],
)
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
@model_validate(WorkflowRunListQuery)
@with_session(write=False)
def get(self, session: Session, req_data: WorkflowRunListQuery, request_context: RequestContext, app_model: App):
"""
Get workflow run list
"""
result = application_services().workflow_runs.get_paginate_workflow_runs(
request_context,
app_id=app_model.id,
args=_workflow_run_list_args(req_data),
triggered_from=_triggered_from(req_data.triggered_from),
)
return dump_response(
WorkflowRunPaginationResponse, workflow_run_pagination_response_source(result, session=session)
)
@console_ns.route("/apps/<uuid:app_id>/workflow-runs/count")
class WorkflowRunCountApi(Resource):
@console_ns.doc("get_workflow_runs_count")
@console_ns.doc(description="Get workflow runs count statistics")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(WorkflowRunCountQuery))
@console_ns.response(
200,
"Workflow runs count retrieved successfully",
console_ns.models[WorkflowRunCountResponse.__name__],
)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())],
)
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
@model_validate(WorkflowRunCountQuery)
def get(self, req_data: WorkflowRunCountQuery, request_context: RequestContext, app_model: App):
"""
Get workflow runs count statistics
"""
result = application_services().workflow_runs.get_workflow_runs_count(
request_context,
app_id=app_model.id,
status=req_data.status,
time_range=req_data.time_range,
triggered_from=_triggered_from(req_data.triggered_from),
)
return dump_response(WorkflowRunCountResponse, result)
@console_ns.route("/apps/<uuid:app_id>/workflow-runs/<uuid:run_id>")
class WorkflowRunDetailApi(Resource):
@console_ns.doc("get_workflow_run_detail")
@console_ns.doc(description="Get workflow run detail")
@console_ns.doc(params={"app_id": "Application ID", "run_id": "Workflow run ID"})
@console_ns.response(
200,
"Workflow run detail retrieved successfully",
console_ns.models[WorkflowRunDetailResponse.__name__],
)
@console_ns.response(404, "Workflow run not found")
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())],
)
@with_session(write=False)
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, session: Session, request_context: RequestContext, app_model: App, run_id: UUID):
"""
Get workflow run detail
"""
workflow_run = application_services().workflow_runs.get_workflow_run(
request_context,
app_id=app_model.id,
run_id=str(run_id),
)
if workflow_run is None:
raise NotFoundError("Workflow run not found")
return dump_response(WorkflowRunDetailResponse, workflow_run_response_source(workflow_run, session=session))
@console_ns.route("/apps/<uuid:app_id>/workflow-runs/<uuid:run_id>/node-executions")
class WorkflowRunNodeExecutionListApi(Resource):
@console_ns.doc("get_workflow_run_node_executions")
@console_ns.doc(description="Get workflow run node execution list")
@console_ns.doc(params={"app_id": "Application ID", "run_id": "Workflow run ID"})
@console_ns.response(
200,
"Node executions retrieved successfully",
console_ns.models[WorkflowRunNodeExecutionListResponse.__name__],
)
@console_ns.response(404, "Workflow run not found")
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())],
)
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, request_context: RequestContext, app_model: App, run_id: UUID):
"""
Get workflow run node execution list
"""
node_executions = application_services().workflow_runs.get_workflow_run_node_executions(
request_context,
app_id=app_model.id,
run_id=str(run_id),
)
return dump_response(WorkflowRunNodeExecutionListResponse, {"data": node_executions})
@console_ns.route("/apps/<uuid:app_id>/workflow-runs/<uuid:run_id>/node-executions/<string:node_execution_id>/children")
class WorkflowToolNodeExecutionListApi(Resource):
@console_ns.doc("get_workflow_tool_node_executions")
@console_ns.doc(description="Get the internal node executions of one workflow tool invocation")
@console_ns.doc(
params={"app_id": "Root application ID", "run_id": "Workflow run ID", "node_execution_id": "Tool execution ID"}
)
@console_ns.response(
200,
"Internal node executions retrieved successfully",
console_ns.models[WorkflowToolNodeExecutionListResponse.__name__],
)
@console_ns.response(404, "Workflow run not found")
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())],
)
@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])
def get(self, request_context: RequestContext, app_model: App, run_id: UUID, node_execution_id: str):
executions = application_services().workflow_runs.get_workflow_tool_node_executions(
request_context,
app_id=app_model.id,
run_id=str(run_id),
node_execution_id=node_execution_id,
)
if executions is None:
raise NotFoundError("Workflow run not found")
try:
# Check every source separately because RBAC check bundles use OR semantics.
for source_app_id in {execution.app_id for execution in executions}:
enforce_rbac_checks(
tenant_id=request_context.active_workspace_id,
account_id=request_context.account_id,
checks=[RBACCheck(RBACPermission.APP_CREATE_AND_MANAGEMENT, PlainApp())],
path_args={"app_id": source_app_id},
)
except Forbidden as error:
raise Forbidden("You do not have permission to view this tool's internal execution details.") from error
return dump_response(WorkflowToolNodeExecutionListResponse, {"data": executions})
@console_ns.route("/workflow/<string:workflow_run_id>/pause-details")
class ConsoleWorkflowPauseDetailsApi(Resource):
"""Console API for getting workflow pause details."""
@console_ns.doc("get_workflow_pause_details")
@console_ns.doc(description="Get workflow pause details")
@console_ns.doc(params={"workflow_run_id": "Workflow run ID"})
@console_ns.response(
200,
"Workflow pause details retrieved successfully",
console_ns.models[WorkflowPauseDetailsResponse.__name__],
)
@console_ns.response(404, "Workflow run not found")
@console_account_admission()
def get(self, request_context: RequestContext, workflow_run_id: str):
"""
Get workflow pause details.
GET /console/api/workflow/<workflow_run_id>/pause-details
Returns information about why and where the workflow is paused.
"""
details = application_services().workflow_runs.get_pause_details(
request_context,
workflow_run_id=workflow_run_id,
)
if details is None:
raise NotFoundError("Workflow run not found")
return (
dump_response(
WorkflowPauseDetailsResponse,
{
"paused_at": details.paused_at.isoformat() + "Z" if details.paused_at else None,
"paused_nodes": [
{
"node_id": node.node_id,
"node_title": node.node_title,
"pause_type": {
"type": "human_input",
"form_id": node.form_id,
"form_version": node.form_version,
"backstage_input_url": _build_backstage_input_url(node.form_token),
},
}
for node in details.paused_nodes
],
},
),
200,
)
@@ -0,0 +1,234 @@
from datetime import datetime
from http import HTTPStatus
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, Field, field_validator
from controllers.common.errors import InvalidArgumentError
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AppNotFoundError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import (
RBACPermission,
model_validate,
)
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from libs.datetime_utils import parse_time_range
from libs.external_api import error_responses
from libs.helper import dump_response
from libs.login import current_account_with_tenant
from machinery.context import RequestContext
from models.model import AppMode
from services.app.console_service import ConsoleAppNotFoundError
class WorkflowStatisticQuery(BaseModel):
start: str | None = Field(default=None, description="Start date and time (YYYY-MM-DD HH:MM)")
end: str | None = Field(default=None, description="End date and time (YYYY-MM-DD HH:MM)")
@field_validator("start", "end", mode="before")
@classmethod
def blank_to_none(cls, value: str | None) -> str | None:
if value == "":
return None
return value
class WorkflowDailyRunsStatisticItem(ResponseModel):
date: str
runs: int
class WorkflowDailyRunsStatisticResponse(ResponseModel):
data: list[WorkflowDailyRunsStatisticItem]
class WorkflowDailyTerminalsStatisticItem(ResponseModel):
date: str
terminal_count: int
class WorkflowDailyTerminalsStatisticResponse(ResponseModel):
data: list[WorkflowDailyTerminalsStatisticItem]
class WorkflowDailyTokenCostStatisticItem(ResponseModel):
date: str
token_count: int
class WorkflowDailyTokenCostStatisticResponse(ResponseModel):
data: list[WorkflowDailyTokenCostStatisticItem]
class WorkflowAverageAppInteractionStatisticItem(ResponseModel):
date: str
interactions: float
class WorkflowAverageAppInteractionStatisticResponse(ResponseModel):
data: list[WorkflowAverageAppInteractionStatisticItem]
register_schema_models(console_ns, WorkflowStatisticQuery)
register_response_schema_models(
console_ns,
WorkflowDailyRunsStatisticResponse,
WorkflowDailyTerminalsStatisticResponse,
WorkflowDailyTokenCostStatisticResponse,
WorkflowAverageAppInteractionStatisticResponse,
)
def _resolve_statistic_time_range(
req_data: WorkflowStatisticQuery,
) -> tuple[datetime | None, datetime | None, str]:
timezone = current_account_with_tenant().account.timezone
assert timezone is not None
try:
start_date, end_date = parse_time_range(req_data.start, req_data.end, timezone)
except ValueError as error:
raise InvalidArgumentError(str(error)) from error
return start_date, end_date, timezone
@console_ns.route("/apps/<uuid:app_id>/workflow/statistics/daily-conversations")
class WorkflowDailyRunsStatistic(Resource):
@console_ns.doc("get_workflow_daily_runs_statistic")
@console_ns.doc(description="Get workflow daily runs statistics")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(WorkflowStatisticQuery))
@console_ns.response(
HTTPStatus.OK,
"Daily runs statistics retrieved successfully",
console_ns.models[WorkflowDailyRunsStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(WorkflowStatisticQuery)
def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_id: UUID) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().workflow_statistics.get_daily_runs(
request_context,
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(WorkflowDailyRunsStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/workflow/statistics/daily-terminals")
class WorkflowDailyTerminalsStatistic(Resource):
@console_ns.doc("get_workflow_daily_terminals_statistic")
@console_ns.doc(description="Get workflow daily terminals statistics")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(WorkflowStatisticQuery))
@console_ns.response(
HTTPStatus.OK,
"Daily terminals statistics retrieved successfully",
console_ns.models[WorkflowDailyTerminalsStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(WorkflowStatisticQuery)
def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_id: UUID) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().workflow_statistics.get_daily_terminals(
request_context,
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(WorkflowDailyTerminalsStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/workflow/statistics/token-costs")
class WorkflowDailyTokenCostStatistic(Resource):
@console_ns.doc("get_workflow_daily_token_cost_statistic")
@console_ns.doc(description="Get workflow daily token cost statistics")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(WorkflowStatisticQuery))
@console_ns.response(
HTTPStatus.OK,
"Daily token cost statistics retrieved successfully",
console_ns.models[WorkflowDailyTokenCostStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(WorkflowStatisticQuery)
def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_id: UUID) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().workflow_statistics.get_daily_token_costs(
request_context,
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(WorkflowDailyTokenCostStatisticResponse, {"data": response_data})
@console_ns.route("/apps/<uuid:app_id>/workflow/statistics/average-app-interactions")
class WorkflowAverageAppInteractionStatistic(Resource):
@console_ns.doc("get_workflow_average_app_interaction_statistic")
@console_ns.doc(description="Get workflow average app interaction statistics")
@console_ns.doc(params={"app_id": "Application ID"})
@console_ns.doc(params=query_params_from_model(WorkflowStatisticQuery))
@console_ns.response(
HTTPStatus.OK,
"Average app interaction statistics retrieved successfully",
console_ns.models[WorkflowAverageAppInteractionStatisticResponse.__name__],
)
@error_responses(AppNotFoundError)
@console_account_admission(
rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],
)
@model_validate(WorkflowStatisticQuery)
def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_id: UUID) -> dict[str, object]:
try:
app = application_services().apps.console.get_reference(request_context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError() from error
supported_modes = (AppMode.WORKFLOW,)
if app.mode not in supported_modes:
mode_values = {mode.value for mode in supported_modes}
raise AppNotFoundError(f"App mode is not in the supported list: {mode_values}")
start_date, end_date, timezone = _resolve_statistic_time_range(req_data)
response_data = application_services().workflow_statistics.get_average_app_interactions(
request_context,
app_id=app.id,
start_date=start_date,
end_date=end_date,
timezone=timezone,
)
return dump_response(WorkflowAverageAppInteractionStatisticResponse, {"data": response_data})
@@ -0,0 +1,166 @@
from dataclasses import asdict
from datetime import datetime
from http import HTTPStatus
from uuid import UUID
from flask_restx import Resource
from pydantic import BaseModel, field_validator
from configs import dify_config
from controllers.common.rbac import PlainApp, RBACCheck
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console import console_ns
from controllers.console.app.error import AppNotFoundError, AppTriggerNotFoundError, WebhookTriggerNotFoundError
from controllers.console.flask_admission import console_account_admission
from controllers.console.wraps import RBACPermission, model_validate
from core.trigger.utils.endpoint import generate_webhook_trigger_endpoint
from enums.account import TenantAccountRole
from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from libs.external_api import error_responses
from libs.helper import dump_response
from machinery.context import RequestContext
from models.enums import AppTriggerType
from services.app.console_service import ConsoleAppNotFoundError
from services.trigger.app_trigger_store import (
AppTriggerNotFoundError as AppTriggerMissing,
)
from services.trigger.app_trigger_store import (
AppTriggerRecord,
)
from services.trigger.app_trigger_store import (
WebhookTriggerNotFoundError as WebhookTriggerMissing,
)
class WebhookTriggerQuery(BaseModel):
node_id: str
class ParserEnable(BaseModel):
trigger_id: str
enable_trigger: bool
class WorkflowTriggerResponse(ResponseModel):
id: str
trigger_type: str
title: str
node_id: str
provider_name: str
icon: str
status: str
created_at: datetime | None = None
updated_at: datetime | None = None
@field_validator("id", "trigger_type", "title", "node_id", "provider_name", "icon", "status", mode="before")
@classmethod
def _normalize_string_fields(cls, value: object) -> str:
if isinstance(value, str):
return value
return str(value)
class WorkflowTriggerListResponse(ResponseModel):
data: list[WorkflowTriggerResponse]
class WebhookTriggerResponse(ResponseModel):
id: str
webhook_id: str
webhook_url: str
webhook_debug_url: str
node_id: str
created_at: datetime | None = None
@field_validator("id", "webhook_id", "webhook_url", "webhook_debug_url", "node_id", mode="before")
@classmethod
def _normalize_string_fields(cls, value: object) -> str:
if isinstance(value, str):
return value
return str(value)
register_schema_models(console_ns, WebhookTriggerQuery, ParserEnable)
register_response_schema_models(
console_ns,
WorkflowTriggerResponse,
WorkflowTriggerListResponse,
WebhookTriggerResponse,
)
def _with_trigger_icon(trigger: AppTriggerRecord) -> dict[str, object]:
icon = ""
if trigger.trigger_type == AppTriggerType.TRIGGER_PLUGIN:
icon = (
dify_config.CONSOLE_API_URL
+ "/console/api/workspaces/current/tool-provider/builtin/"
+ (trigger.provider_name or "")
+ "/icon"
)
return {**asdict(trigger), "icon": icon}
@console_ns.route("/apps/<uuid:app_id>/workflows/triggers/webhook")
class WebhookTriggerApi(Resource):
@error_responses(AppNotFoundError, WebhookTriggerNotFoundError)
@console_ns.doc(params=query_params_from_model(WebhookTriggerQuery))
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[WebhookTriggerResponse.__name__])
@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp())])
@model_validate(WebhookTriggerQuery)
def get(self, req_data: WebhookTriggerQuery, context: RequestContext, app_id: UUID) -> dict[str, object]:
"""Get webhook trigger for a node"""
try:
webhook = application_services().apps.triggers.get_webhook(context, str(app_id), req_data.node_id)
except ConsoleAppNotFoundError as error:
raise AppNotFoundError(str(error)) from error
except WebhookTriggerMissing as error:
raise WebhookTriggerNotFoundError(str(error)) from error
return dump_response(
WebhookTriggerResponse,
{
**asdict(webhook),
"webhook_url": generate_webhook_trigger_endpoint(webhook.webhook_id),
"webhook_debug_url": generate_webhook_trigger_endpoint(webhook.webhook_id, debug=True),
},
)
@console_ns.route("/apps/<uuid:app_id>/triggers")
class AppTriggersApi(Resource):
@error_responses(AppNotFoundError)
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[WorkflowTriggerListResponse.__name__])
@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_VIEW_LAYOUT, PlainApp())])
def get(self, context: RequestContext, app_id: UUID) -> dict[str, object]:
"""Get app triggers list"""
try:
triggers = application_services().apps.triggers.list_triggers(context, str(app_id))
except ConsoleAppNotFoundError as error:
raise AppNotFoundError(str(error)) from error
return dump_response(
WorkflowTriggerListResponse, {"data": [_with_trigger_icon(trigger) for trigger in triggers]}
)
@console_ns.route("/apps/<uuid:app_id>/trigger-enable")
class AppTriggerEnableApi(Resource):
@error_responses(AppNotFoundError, AppTriggerNotFoundError)
@console_ns.expect(console_ns.models[ParserEnable.__name__])
@console_ns.response(HTTPStatus.OK, "Success", console_ns.models[WorkflowTriggerResponse.__name__])
@console_account_admission(
allowed_roles=frozenset({TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}),
rbac_checks=[RBACCheck(RBACPermission.APP_EDIT, PlainApp())],
)
@model_validate(ParserEnable)
def post(self, req_data: ParserEnable, context: RequestContext, app_id: UUID) -> dict[str, object]:
"""Update app trigger (enable/disable)"""
try:
trigger = application_services().apps.triggers.set_enabled(
context, str(app_id), req_data.trigger_id, enabled=req_data.enable_trigger
)
except ConsoleAppNotFoundError as error:
raise AppNotFoundError(str(error)) from error
except AppTriggerMissing as error:
raise AppTriggerNotFoundError(str(error)) from error
return dump_response(WorkflowTriggerResponse, _with_trigger_icon(trigger))
@@ -0,0 +1,135 @@
"""Controller decorators for console app resources.
`get_app_model` still supports legacy handlers backed by Flask-SQLAlchemy's
scoped session. Public catalog previews use `app.preview_admission` and
detached application queries.
"""
from collections.abc import Callable
from functools import wraps
from typing import cast, overload
from sqlalchemy import select
from sqlalchemy.orm import Session, scoped_session
from controllers.common.session import with_session
from controllers.console.app.error import AppNotFoundError
from extensions.ext_database import db
from libs.external_api import document_errors
from libs.login import current_account_with_tenant
from models import App, AppMode
from models.agent import AgentScope
from models.enums import AppStatus
__all__ = [
"get_app_model",
"with_session",
]
def _load_app_model(session: Session | scoped_session, app_id: str) -> App | None:
"""Load the tenant-scoped Console app row through the given session."""
_, current_tenant_id = current_account_with_tenant()
app_model = session.scalar(
select(App)
.where(
App.id == app_id,
App.tenant_id == current_tenant_id,
App.status == AppStatus.NORMAL,
)
.limit(1)
)
if app_model is not None:
binding = app_model.agent_app_binding_with_session(session=session, include_archived=True)
if binding is not None and binding.scope == AgentScope.WORKFLOW_ONLY:
return None
return app_model
def _load_app_model_from_scoped_session(app_id: str) -> App | None:
"""Load the app row for legacy handlers that have not adopted request session injection yet."""
return _load_app_model(db.session, app_id)
def _get_injected_session(args: tuple[object, ...]) -> Session | None:
"""Return the request session inserted by `with_session`, if this handler has been migrated."""
if len(args) < 2:
return None
candidate = args[1]
if isinstance(candidate, Session):
return candidate
if hasattr(candidate, "scalar") and hasattr(candidate, "commit") and hasattr(candidate, "rollback"):
return cast(Session, candidate)
return None
@overload
def get_app_model[**P, R](
view: Callable[P, R],
*,
mode: AppMode | list[AppMode] | None = None,
) -> Callable[P, R]: ...
@overload
def get_app_model[**P, R](
view: None = None,
*,
mode: AppMode | list[AppMode] | None = None,
) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
def get_app_model[**P, R](
view: Callable[P, R] | None = None,
*,
mode: AppMode | list[AppMode] | None = None,
) -> Callable[P, R] | Callable[[Callable[P, R]], Callable[P, R]]:
"""Inject the App model for handlers that receive an `app_id` path parameter.
New handlers may compose `@with_session` above this decorator so the app row
is loaded through the same request-scoped session used by the controller.
Existing handlers continue to work through `db.session` until migrated.
"""
def decorator(view_func: Callable[P, R]) -> Callable[P, R]:
@wraps(view_func)
def decorated_view(*args: P.args, **kwargs: P.kwargs) -> R:
if not kwargs.get("app_id"):
raise ValueError("missing app_id in path parameters")
app_id = kwargs.get("app_id")
app_id = str(app_id)
del kwargs["app_id"]
session = _get_injected_session(args)
if session is None:
app_model = _load_app_model_from_scoped_session(app_id)
else:
app_model = _load_app_model(session, app_id)
if not app_model:
raise AppNotFoundError()
app_mode = AppMode.value_of(app_model.mode)
if mode is not None:
modes = mode if isinstance(mode, list) else [mode]
if app_mode not in modes:
mode_values = {m.value for m in modes}
raise AppNotFoundError(f"App mode is not in the supported list: {mode_values}")
kwargs["app_model"] = app_model
return view_func(*args, **kwargs)
return document_errors(decorated_view, AppNotFoundError)
if view is None:
return decorator
else:
return decorator(view)