| import logging | import logging | ||||
| from flask_restful import Resource, reqparse | |||||
| from flask_restful import Resource, fields, marshal_with, reqparse | |||||
| from werkzeug.exceptions import InternalServerError | from werkzeug.exceptions import InternalServerError | ||||
| from controllers.service_api import api | from controllers.service_api import api | ||||
| QuotaExceededError, | QuotaExceededError, | ||||
| ) | ) | ||||
| from core.model_runtime.errors.invoke import InvokeError | from core.model_runtime.errors.invoke import InvokeError | ||||
| from extensions.ext_database import db | |||||
| from libs import helper | from libs import helper | ||||
| from models.model import App, AppMode, EndUser | from models.model import App, AppMode, EndUser | ||||
| from models.workflow import WorkflowRun | |||||
| from services.app_generate_service import AppGenerateService | from services.app_generate_service import AppGenerateService | ||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
| class WorkflowRunApi(Resource): | class WorkflowRunApi(Resource): | ||||
| 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': fields.DateTime, | |||||
| 'finished_at': fields.DateTime, | |||||
| 'elapsed_time': fields.Float, | |||||
| } | |||||
| @validate_app_token | |||||
| @marshal_with(workflow_run_fields) | |||||
| def get(self, app_model: App, workflow_id: str): | |||||
| """ | |||||
| Get a workflow task running detail | |||||
| """ | |||||
| app_mode = AppMode.value_of(app_model.mode) | |||||
| if app_mode != AppMode.WORKFLOW: | |||||
| raise NotWorkflowAppError() | |||||
| workflow_run = db.session.query(WorkflowRun).filter(WorkflowRun.id == workflow_id).first() | |||||
| return workflow_run | |||||
| @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON, required=True)) | @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON, required=True)) | ||||
| def post(self, app_model: App, end_user: EndUser): | def post(self, app_model: App, end_user: EndUser): | ||||
| """ | """ | ||||
| } | } | ||||
| api.add_resource(WorkflowRunApi, '/workflows/run') | |||||
| api.add_resource(WorkflowRunApi, '/workflows/run/<string:workflow_id>', '/workflows/run') | |||||
| api.add_resource(WorkflowTaskStopApi, '/workflows/tasks/<string:task_id>/stop') | api.add_resource(WorkflowTaskStopApi, '/workflows/tasks/<string:task_id>/stop') |
| --- | --- | ||||
| <Heading | |||||
| url='/workflows/run/:workflow_id' | |||||
| method='GET' | |||||
| title='Get Workflow Run Detail' | |||||
| name='#get-workflow-run-detail' | |||||
| /> | |||||
| <Row> | |||||
| <Col> | |||||
| Retrieve the current execution results of a workflow task based on the workflow execution ID. | |||||
| ### Path | |||||
| - `workflow_id` (string) Workflow ID, can be obtained from the streaming chunk return | |||||
| ### Response | |||||
| - `id` (string) ID of workflow execution | |||||
| - `workflow_id` (string) ID of relatied workflow | |||||
| - `status` (string) status of execution, `running` / `succeeded` / `failed` / `stopped` | |||||
| - `inputs` (json) content of input | |||||
| - `outputs` (json) content of output | |||||
| - `error` (string) reason of error | |||||
| - `total_steps` (int) total steps of task | |||||
| - `total_tokens` (int) total tokens to be used | |||||
| - `created_at` (timestamp) start time | |||||
| - `finished_at` (timestamp) end time | |||||
| - `elapsed_time` (float) total seconds to be used | |||||
| </Col> | |||||
| <Col sticky> | |||||
| ### Request Example | |||||
| <CodeGroup title="Request" tag="GET" label="/workflows/run/:workflow_id" targetCode={`curl -X GET '${props.appDetail.api_base_url}/workflows/run/:workflow_id' \\\n-H 'Authorization: Bearer {api_key}' \\\n-H 'Content-Type: application/json'`}> | |||||
| ```bash {{ title: 'cURL' }} | |||||
| curl -X GET '${props.appDetail.api_base_url}/workflows/run/:workflow_id' \ | |||||
| -H 'Authorization: Bearer {api_key}' \ | |||||
| -H 'Content-Type: application/json' | |||||
| ``` | |||||
| </CodeGroup> | |||||
| ### Response Example | |||||
| <CodeGroup title="Response"> | |||||
| ```json {{ title: 'Response' }} | |||||
| { | |||||
| "id": "b1ad3277-089e-42c6-9dff-6820d94fbc76", | |||||
| "workflow_id": "19eff89f-ec03-4f75-b0fc-897e7effea02", | |||||
| "status": "succeeded", | |||||
| "inputs": "{\"sys.files\": [], \"sys.user_id\": \"abc-123\"}", | |||||
| "outputs": null, | |||||
| "error": null, | |||||
| "total_steps": 3, | |||||
| "total_tokens": 0, | |||||
| "created_at": "Thu, 18 Jul 2024 03:17:40 -0000", | |||||
| "finished_at": "Thu, 18 Jul 2024 03:18:10 -0000", | |||||
| "elapsed_time": 30.098514399956912 | |||||
| } | |||||
| ``` | |||||
| </CodeGroup> | |||||
| </Col> | |||||
| </Row> | |||||
| --- | |||||
| <Heading | <Heading | ||||
| url='/workflows/tasks/:task_id/stop' | url='/workflows/tasks/:task_id/stop' | ||||
| method='POST' | method='POST' |
| --- | --- | ||||
| <Heading | |||||
| url='/workflows/run/:workflow_id' | |||||
| method='GET' | |||||
| title='获取workflow执行情况' | |||||
| name='#get-workflow-run-detail' | |||||
| /> | |||||
| <Row> | |||||
| <Col> | |||||
| 根据 workflow 执行 ID 获取 workflow 任务当前执行结果 | |||||
| ### Path | |||||
| - `workflow_id` (string) workflow 执行 ID,可在流式返回 Chunk 中获取 | |||||
| ### Response | |||||
| - `id` (string) workflow 执行 ID | |||||
| - `workflow_id` (string) 关联的 Workflow ID | |||||
| - `status` (string) 执行状态 `running` / `succeeded` / `failed` / `stopped` | |||||
| - `inputs` (json) 任务输入内容 | |||||
| - `outputs` (json) 任务输出内容 | |||||
| - `error` (string) 错误原因 | |||||
| - `total_steps` (int) 任务执行总步数 | |||||
| - `total_tokens` (int) 任务执行总 tokens | |||||
| - `created_at` (timestamp) 任务开始时间 | |||||
| - `finished_at` (timestamp) 任务结束时间 | |||||
| - `elapsed_time` (float) 耗时(s) | |||||
| </Col> | |||||
| <Col sticky> | |||||
| ### Request Example | |||||
| <CodeGroup title="Request" tag="GET" label="/workflows/run/:workflow_id" targetCode={`curl -X GET '${props.appDetail.api_base_url}/workflows/run/:workflow_id' \\\n-H 'Authorization: Bearer {api_key}' \\\n-H 'Content-Type: application/json'`}> | |||||
| ```bash {{ title: 'cURL' }} | |||||
| curl -X GET '${props.appDetail.api_base_url}/workflows/run/:workflow_id' \ | |||||
| -H 'Authorization: Bearer {api_key}' \ | |||||
| -H 'Content-Type: application/json' | |||||
| ``` | |||||
| </CodeGroup> | |||||
| ### Response Example | |||||
| <CodeGroup title="Response"> | |||||
| ```json {{ title: 'Response' }} | |||||
| { | |||||
| "id": "b1ad3277-089e-42c6-9dff-6820d94fbc76", | |||||
| "workflow_id": "19eff89f-ec03-4f75-b0fc-897e7effea02", | |||||
| "status": "succeeded", | |||||
| "inputs": "{\"sys.files\": [], \"sys.user_id\": \"abc-123\"}", | |||||
| "outputs": null, | |||||
| "error": null, | |||||
| "total_steps": 3, | |||||
| "total_tokens": 0, | |||||
| "created_at": "Thu, 18 Jul 2024 03:17:40 -0000", | |||||
| "finished_at": "Thu, 18 Jul 2024 03:18:10 -0000", | |||||
| "elapsed_time": 30.098514399956912 | |||||
| } | |||||
| ``` | |||||
| </CodeGroup> | |||||
| </Col> | |||||
| </Row> | |||||
| --- | |||||
| <Heading | <Heading | ||||
| url='/workflows/tasks/:task_id/stop' | url='/workflows/tasks/:task_id/stop' | ||||
| method='POST' | method='POST' |