mirror of
https://github.com/YFGaia/dify-plus.git
synced 2026-06-04 10:14:00 +08:00
b2f5707676
# Conflicts: # .github/workflows/tool-test-sdks.yaml # api/.env.example # api/README.md # api/commands.py # api/controllers/console/explore/wraps.py # api/controllers/web/workflow.py # api/extensions/ext_commands.py # api/models/model.py # api/pyproject.toml # api/services/feature_service.py # web/README.md # web/app/components/explore/app-card/index.tsx # web/app/components/explore/app-list/index.tsx # web/app/components/explore/sidebar/index.tsx # web/app/signin/components/mail-and-password-auth.tsx # web/i18n/uk-UA/app-overview.json # web/i18n/uk-UA/app.json # web/i18n/uk-UA/billing.json # web/i18n/uk-UA/common.json # web/i18n/uk-UA/dataset-creation.json # web/i18n/uk-UA/dataset-documents.json # web/i18n/uk-UA/dataset-hit-testing.json # web/i18n/uk-UA/dataset-settings.json # web/i18n/uk-UA/dataset.json # web/i18n/uk-UA/explore.json # web/i18n/uk-UA/plugin.json # web/i18n/uk-UA/tools.json # web/next.config.js # web/package.json # web/pnpm-lock.yaml # web/service/common.ts # web/service/explore.ts # web/service/fetch.ts # web/service/use-explore.ts # web/types/feature.ts
162 lines
5.7 KiB
Python
162 lines
5.7 KiB
Python
import logging
|
|
from typing import Any
|
|
|
|
from pydantic import BaseModel, Field
|
|
from werkzeug.exceptions import InternalServerError
|
|
|
|
from controllers.common.schema import register_schema_models
|
|
from controllers.web import web_ns
|
|
from controllers.web.error import (
|
|
CompletionRequestError,
|
|
NotWorkflowAppError,
|
|
ProviderModelCurrentlyNotSupportError,
|
|
ProviderNotInitializeError,
|
|
ProviderQuotaExceededError,
|
|
)
|
|
from controllers.web.error import InvokeRateLimitError as InvokeRateLimitHttpError
|
|
from controllers.web.wraps import WebApiResource
|
|
from core.app.apps.base_app_queue_manager import AppQueueManager
|
|
from core.app.entities.app_invoke_entities import InvokeFrom
|
|
from core.errors.error import (
|
|
ModelCurrentlyNotSupportError,
|
|
ProviderTokenNotInitError,
|
|
QuotaExceededError,
|
|
)
|
|
from core.model_runtime.errors.invoke import InvokeError
|
|
from core.workflow.graph_engine.manager import GraphEngineManager
|
|
from libs import helper
|
|
from models.model import App, AppMode, EndUser
|
|
from services.app_generate_service import AppGenerateService
|
|
from services.errors.llm import InvokeRateLimitError
|
|
|
|
|
|
class WorkflowRunPayload(BaseModel):
|
|
inputs: dict[str, Any] = Field(description="Input variables for the workflow")
|
|
files: list[dict[str, Any]] | None = Field(default=None, description="Files to be processed by the workflow")
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# extend: start 您必须登录才能访问您的帐户扩展功能
|
|
from controllers.web.completion import is_end_login, is_money_limit
|
|
from controllers.web.error_extend import (
|
|
AccountNoMoneyErrorExtend,
|
|
WebAuthRequiredErrorExtend,
|
|
)
|
|
from services.app_generate_service_extend import AppGenerateServiceExtend
|
|
|
|
# extend: stop 您必须登录才能访问您的帐户扩展功能
|
|
|
|
register_schema_models(web_ns, WorkflowRunPayload)
|
|
|
|
|
|
@web_ns.route("/workflows/run")
|
|
class WorkflowRunApi(WebApiResource):
|
|
@web_ns.doc("Run Workflow")
|
|
@web_ns.doc(description="Execute a workflow with provided inputs and files.")
|
|
@web_ns.expect(web_ns.models[WorkflowRunPayload.__name__])
|
|
@web_ns.doc(
|
|
responses={
|
|
200: "Success",
|
|
400: "Bad Request",
|
|
401: "Unauthorized",
|
|
403: "Forbidden",
|
|
404: "App Not Found",
|
|
500: "Internal Server Error",
|
|
}
|
|
)
|
|
def post(self, app_model: App, end_user: EndUser):
|
|
"""
|
|
Run workflow
|
|
"""
|
|
app_mode = AppMode.value_of(app_model.mode)
|
|
if app_mode != AppMode.WORKFLOW:
|
|
raise NotWorkflowAppError()
|
|
|
|
# ----------------- start You must log in to access your account extend ---------------
|
|
# no login
|
|
if is_end_login(end_user) is None:
|
|
raise WebAuthRequiredErrorExtend()
|
|
# ----------------- stop You must log in to access your account extend ---------------
|
|
|
|
# ----------------- 二开部分Begin - 余额判断-----------------
|
|
if is_money_limit(end_user):
|
|
raise AccountNoMoneyErrorExtend()
|
|
# ----------------- 二开部分End - 余额判断-----------------
|
|
|
|
parser = (
|
|
reqparse.RequestParser()
|
|
.add_argument("inputs", type=dict, required=True, nullable=False, location="json")
|
|
.add_argument("files", type=list, required=False, location="json")
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
# extend: 获取 Console 用户 ID,直接作为 from_account_id 传递
|
|
user_info = is_end_login(end_user)
|
|
if user_info:
|
|
args["account_id"] = user_info.id
|
|
|
|
try:
|
|
AppGenerateServiceExtend.calculate_cumulative_usage(
|
|
app_model=app_model,
|
|
args=args,
|
|
) # Extend: App
|
|
# Center - Recommended list sorted by usage frequency
|
|
response = AppGenerateService.generate(
|
|
app_model=app_model, user=end_user, args=args, invoke_from=InvokeFrom.WEB_APP, streaming=True
|
|
)
|
|
|
|
return helper.compact_generate_response(response)
|
|
except ProviderTokenNotInitError as ex:
|
|
raise ProviderNotInitializeError(ex.description)
|
|
except QuotaExceededError:
|
|
raise ProviderQuotaExceededError()
|
|
except ModelCurrentlyNotSupportError:
|
|
raise ProviderModelCurrentlyNotSupportError()
|
|
except InvokeError as e:
|
|
raise CompletionRequestError(e.description)
|
|
except InvokeRateLimitError as ex:
|
|
raise InvokeRateLimitHttpError(ex.description)
|
|
except ValueError as e:
|
|
raise e
|
|
except Exception:
|
|
logger.exception("internal server error.")
|
|
raise InternalServerError()
|
|
|
|
|
|
@web_ns.route("/workflows/tasks/<string:task_id>/stop")
|
|
class WorkflowTaskStopApi(WebApiResource):
|
|
@web_ns.doc("Stop Workflow Task")
|
|
@web_ns.doc(description="Stop a running workflow task.")
|
|
@web_ns.doc(
|
|
params={
|
|
"task_id": {"description": "Task ID to stop", "type": "string", "required": True},
|
|
}
|
|
)
|
|
@web_ns.doc(
|
|
responses={
|
|
200: "Success",
|
|
400: "Bad Request",
|
|
401: "Unauthorized",
|
|
403: "Forbidden",
|
|
404: "Task Not Found",
|
|
500: "Internal Server Error",
|
|
}
|
|
)
|
|
def post(self, app_model: App, end_user: EndUser, task_id: str):
|
|
"""
|
|
Stop workflow task
|
|
"""
|
|
app_mode = AppMode.value_of(app_model.mode)
|
|
if app_mode != AppMode.WORKFLOW:
|
|
raise NotWorkflowAppError()
|
|
|
|
# Stop using both mechanisms for backward compatibility
|
|
# Legacy stop flag mechanism (without user check)
|
|
AppQueueManager.set_stop_flag_no_user_check(task_id)
|
|
|
|
# New graph engine command channel mechanism
|
|
GraphEngineManager.send_stop_command(task_id)
|
|
|
|
return {"result": "success"}
|