mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
314 lines
12 KiB
Python
314 lines
12 KiB
Python
import logging
|
|
|
|
from dateutil.parser import isoparse
|
|
from flask import request
|
|
from flask_restx import Api, Namespace, Resource, fields, reqparse
|
|
from flask_restx.inputs import int_range
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
from werkzeug.exceptions import BadRequest, InternalServerError, NotFound
|
|
|
|
from controllers.service_api import service_api_ns
|
|
from controllers.service_api.app.error import (
|
|
CompletionRequestError,
|
|
NotWorkflowAppError,
|
|
ProviderModelCurrentlyNotSupportError,
|
|
ProviderNotInitializeError,
|
|
ProviderQuotaExceededError,
|
|
)
|
|
from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token
|
|
from controllers.web.error import InvokeRateLimitError as InvokeRateLimitHttpError
|
|
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.helper.trace_id_helper import get_external_trace_id
|
|
from core.model_runtime.errors.invoke import InvokeError
|
|
from core.workflow.entities.workflow_execution import WorkflowExecutionStatus
|
|
from extensions.ext_database import db
|
|
from fields.workflow_app_log_fields import build_workflow_app_log_pagination_model
|
|
from libs import helper
|
|
from libs.helper import TimestampField
|
|
from models.model import App, AppMode, EndUser
|
|
from repositories.factory import DifyAPIRepositoryFactory
|
|
from services.app_generate_service import AppGenerateService
|
|
from services.errors.app import IsDraftWorkflowError, WorkflowIdFormatError, WorkflowNotFoundError
|
|
from services.errors.llm import InvokeRateLimitError
|
|
from services.workflow_app_service import WorkflowAppService
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Define parsers for workflow APIs
|
|
workflow_run_parser = reqparse.RequestParser()
|
|
workflow_run_parser.add_argument("inputs", type=dict, required=True, nullable=False, location="json")
|
|
workflow_run_parser.add_argument("files", type=list, required=False, location="json")
|
|
workflow_run_parser.add_argument("response_mode", type=str, choices=["blocking", "streaming"], location="json")
|
|
|
|
workflow_log_parser = reqparse.RequestParser()
|
|
workflow_log_parser.add_argument("keyword", type=str, location="args")
|
|
workflow_log_parser.add_argument("status", type=str, choices=["succeeded", "failed", "stopped"], location="args")
|
|
workflow_log_parser.add_argument("created_at__before", type=str, location="args")
|
|
workflow_log_parser.add_argument("created_at__after", type=str, location="args")
|
|
workflow_log_parser.add_argument(
|
|
"created_by_end_user_session_id",
|
|
type=str,
|
|
location="args",
|
|
required=False,
|
|
default=None,
|
|
)
|
|
workflow_log_parser.add_argument(
|
|
"created_by_account",
|
|
type=str,
|
|
location="args",
|
|
required=False,
|
|
default=None,
|
|
)
|
|
workflow_log_parser.add_argument("page", type=int_range(1, 99999), default=1, location="args")
|
|
workflow_log_parser.add_argument("limit", type=int_range(1, 100), default=20, location="args")
|
|
|
|
workflow_run_fields = {
|
|
"id": fields.String,
|
|
"workflow_id": fields.String,
|
|
"status": fields.String,
|
|
"inputs": fields.Raw,
|
|
"outputs": fields.Raw,
|
|
"error": fields.String,
|
|
"total_steps": fields.Integer,
|
|
"total_tokens": fields.Integer,
|
|
"created_at": TimestampField,
|
|
"finished_at": TimestampField,
|
|
"elapsed_time": fields.Float,
|
|
}
|
|
|
|
|
|
def build_workflow_run_model(api_or_ns: Api | Namespace):
|
|
"""Build the workflow run model for the API or Namespace."""
|
|
return api_or_ns.model("WorkflowRun", workflow_run_fields)
|
|
|
|
|
|
@service_api_ns.route("/workflows/run/<string:workflow_run_id>")
|
|
class WorkflowRunDetailApi(Resource):
|
|
@service_api_ns.doc("get_workflow_run_detail")
|
|
@service_api_ns.doc(description="Get workflow run details")
|
|
@service_api_ns.doc(params={"workflow_run_id": "Workflow run ID"})
|
|
@service_api_ns.doc(
|
|
responses={
|
|
200: "Workflow run details retrieved successfully",
|
|
401: "Unauthorized - invalid API token",
|
|
404: "Workflow run not found",
|
|
}
|
|
)
|
|
@validate_app_token
|
|
@service_api_ns.marshal_with(build_workflow_run_model(service_api_ns))
|
|
def get(self, app_model: App, workflow_run_id: str):
|
|
"""Get a workflow task running detail.
|
|
|
|
Returns detailed information about a specific workflow run.
|
|
"""
|
|
app_mode = AppMode.value_of(app_model.mode)
|
|
if app_mode not in [AppMode.WORKFLOW, AppMode.ADVANCED_CHAT]:
|
|
raise NotWorkflowAppError()
|
|
|
|
# Use repository to get workflow run
|
|
session_maker = sessionmaker(bind=db.engine, expire_on_commit=False)
|
|
workflow_run_repo = DifyAPIRepositoryFactory.create_api_workflow_run_repository(session_maker)
|
|
|
|
workflow_run = workflow_run_repo.get_workflow_run_by_id(
|
|
tenant_id=app_model.tenant_id,
|
|
app_id=app_model.id,
|
|
run_id=workflow_run_id,
|
|
)
|
|
return workflow_run
|
|
|
|
|
|
@service_api_ns.route("/workflows/run")
|
|
class WorkflowRunApi(Resource):
|
|
@service_api_ns.expect(workflow_run_parser)
|
|
@service_api_ns.doc("run_workflow")
|
|
@service_api_ns.doc(description="Execute a workflow")
|
|
@service_api_ns.doc(
|
|
responses={
|
|
200: "Workflow executed successfully",
|
|
400: "Bad request - invalid parameters or workflow issues",
|
|
401: "Unauthorized - invalid API token",
|
|
404: "Workflow not found",
|
|
429: "Rate limit exceeded",
|
|
500: "Internal server error",
|
|
}
|
|
)
|
|
@validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON, required=True))
|
|
def post(self, app_model: App, end_user: EndUser):
|
|
"""Execute a workflow.
|
|
|
|
Runs a workflow with the provided inputs and returns the results.
|
|
Supports both blocking and streaming response modes.
|
|
"""
|
|
app_mode = AppMode.value_of(app_model.mode)
|
|
if app_mode != AppMode.WORKFLOW:
|
|
raise NotWorkflowAppError()
|
|
|
|
args = workflow_run_parser.parse_args()
|
|
external_trace_id = get_external_trace_id(request)
|
|
if external_trace_id:
|
|
args["external_trace_id"] = external_trace_id
|
|
streaming = args.get("response_mode") == "streaming"
|
|
|
|
try:
|
|
response = AppGenerateService.generate(
|
|
app_model=app_model, user=end_user, args=args, invoke_from=InvokeFrom.SERVICE_API, streaming=streaming
|
|
)
|
|
|
|
return helper.compact_generate_response(response)
|
|
except ProviderTokenNotInitError as ex:
|
|
raise ProviderNotInitializeError(ex.description)
|
|
except QuotaExceededError:
|
|
raise ProviderQuotaExceededError()
|
|
except ModelCurrentlyNotSupportError:
|
|
raise ProviderModelCurrentlyNotSupportError()
|
|
except InvokeRateLimitError as ex:
|
|
raise InvokeRateLimitHttpError(ex.description)
|
|
except InvokeError as e:
|
|
raise CompletionRequestError(e.description)
|
|
except ValueError as e:
|
|
raise e
|
|
except Exception:
|
|
logger.exception("internal server error.")
|
|
raise InternalServerError()
|
|
|
|
|
|
@service_api_ns.route("/workflows/<string:workflow_id>/run")
|
|
class WorkflowRunByIdApi(Resource):
|
|
@service_api_ns.expect(workflow_run_parser)
|
|
@service_api_ns.doc("run_workflow_by_id")
|
|
@service_api_ns.doc(description="Execute a specific workflow by ID")
|
|
@service_api_ns.doc(params={"workflow_id": "Workflow ID to execute"})
|
|
@service_api_ns.doc(
|
|
responses={
|
|
200: "Workflow executed successfully",
|
|
400: "Bad request - invalid parameters or workflow issues",
|
|
401: "Unauthorized - invalid API token",
|
|
404: "Workflow not found",
|
|
429: "Rate limit exceeded",
|
|
500: "Internal server error",
|
|
}
|
|
)
|
|
@validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON, required=True))
|
|
def post(self, app_model: App, end_user: EndUser, workflow_id: str):
|
|
"""Run specific workflow by ID.
|
|
|
|
Executes a specific workflow version identified by its ID.
|
|
"""
|
|
app_mode = AppMode.value_of(app_model.mode)
|
|
if app_mode != AppMode.WORKFLOW:
|
|
raise NotWorkflowAppError()
|
|
|
|
args = workflow_run_parser.parse_args()
|
|
|
|
# Add workflow_id to args for AppGenerateService
|
|
args["workflow_id"] = workflow_id
|
|
|
|
external_trace_id = get_external_trace_id(request)
|
|
if external_trace_id:
|
|
args["external_trace_id"] = external_trace_id
|
|
streaming = args.get("response_mode") == "streaming"
|
|
|
|
try:
|
|
response = AppGenerateService.generate(
|
|
app_model=app_model, user=end_user, args=args, invoke_from=InvokeFrom.SERVICE_API, streaming=streaming
|
|
)
|
|
|
|
return helper.compact_generate_response(response)
|
|
except WorkflowNotFoundError as ex:
|
|
raise NotFound(str(ex))
|
|
except IsDraftWorkflowError as ex:
|
|
raise BadRequest(str(ex))
|
|
except WorkflowIdFormatError as ex:
|
|
raise BadRequest(str(ex))
|
|
except ProviderTokenNotInitError as ex:
|
|
raise ProviderNotInitializeError(ex.description)
|
|
except QuotaExceededError:
|
|
raise ProviderQuotaExceededError()
|
|
except ModelCurrentlyNotSupportError:
|
|
raise ProviderModelCurrentlyNotSupportError()
|
|
except InvokeRateLimitError as ex:
|
|
raise InvokeRateLimitHttpError(ex.description)
|
|
except InvokeError as e:
|
|
raise CompletionRequestError(e.description)
|
|
except ValueError as e:
|
|
raise e
|
|
except Exception:
|
|
logger.exception("internal server error.")
|
|
raise InternalServerError()
|
|
|
|
|
|
@service_api_ns.route("/workflows/tasks/<string:task_id>/stop")
|
|
class WorkflowTaskStopApi(Resource):
|
|
@service_api_ns.doc("stop_workflow_task")
|
|
@service_api_ns.doc(description="Stop a running workflow task")
|
|
@service_api_ns.doc(params={"task_id": "Task ID to stop"})
|
|
@service_api_ns.doc(
|
|
responses={
|
|
200: "Task stopped successfully",
|
|
401: "Unauthorized - invalid API token",
|
|
404: "Task not found",
|
|
}
|
|
)
|
|
@validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON, required=True))
|
|
def post(self, app_model: App, end_user: EndUser, task_id: str):
|
|
"""Stop a running workflow task."""
|
|
app_mode = AppMode.value_of(app_model.mode)
|
|
if app_mode != AppMode.WORKFLOW:
|
|
raise NotWorkflowAppError()
|
|
|
|
AppQueueManager.set_stop_flag(task_id, InvokeFrom.SERVICE_API, end_user.id)
|
|
|
|
return {"result": "success"}
|
|
|
|
|
|
@service_api_ns.route("/workflows/logs")
|
|
class WorkflowAppLogApi(Resource):
|
|
@service_api_ns.expect(workflow_log_parser)
|
|
@service_api_ns.doc("get_workflow_logs")
|
|
@service_api_ns.doc(description="Get workflow execution logs")
|
|
@service_api_ns.doc(
|
|
responses={
|
|
200: "Logs retrieved successfully",
|
|
401: "Unauthorized - invalid API token",
|
|
}
|
|
)
|
|
@validate_app_token
|
|
@service_api_ns.marshal_with(build_workflow_app_log_pagination_model(service_api_ns))
|
|
def get(self, app_model: App):
|
|
"""Get workflow app logs.
|
|
|
|
Returns paginated workflow execution logs with filtering options.
|
|
"""
|
|
args = workflow_log_parser.parse_args()
|
|
|
|
args.status = WorkflowExecutionStatus(args.status) if args.status else None
|
|
if args.created_at__before:
|
|
args.created_at__before = isoparse(args.created_at__before)
|
|
|
|
if args.created_at__after:
|
|
args.created_at__after = isoparse(args.created_at__after)
|
|
|
|
# get paginate workflow app logs
|
|
workflow_app_service = WorkflowAppService()
|
|
with Session(db.engine) as session:
|
|
workflow_app_log_pagination = workflow_app_service.get_paginate_workflow_app_logs(
|
|
session=session,
|
|
app_model=app_model,
|
|
keyword=args.keyword,
|
|
status=args.status,
|
|
created_at_before=args.created_at__before,
|
|
created_at_after=args.created_at__after,
|
|
page=args.page,
|
|
limit=args.limit,
|
|
created_by_end_user_session_id=args.created_by_end_user_session_id,
|
|
created_by_account=args.created_by_account,
|
|
)
|
|
|
|
return workflow_app_log_pagination
|