feat(workflow): add configurable workflow feature options
This commit is contained in:
@@ -570,6 +570,9 @@ class WorkflowService:
|
||||
message=f"工作流配置不存在: app_id={app_id}"
|
||||
)
|
||||
|
||||
feature_configs = config.features or {}
|
||||
self._validate_file_upload(feature_configs, payload.files)
|
||||
|
||||
input_data = {
|
||||
"message": payload.message, "variables": payload.variables,
|
||||
"conversation_id": payload.conversation_id,
|
||||
@@ -737,6 +740,8 @@ class WorkflowService:
|
||||
code=BizCode.CONFIG_MISSING,
|
||||
message=f"工作流配置不存在: app_id={app_id}"
|
||||
)
|
||||
feature_configs = config.features or {}
|
||||
self._validate_file_upload(feature_configs, payload.files)
|
||||
|
||||
input_data = {
|
||||
"message": payload.message, "variables": payload.variables,
|
||||
@@ -845,7 +850,10 @@ class WorkflowService:
|
||||
yield event
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"工作流流式执行失败: execution_id={execution.execution_id}, error={e}", exc_info=True)
|
||||
logger.error(
|
||||
f"Workflow streaming execution failed: execution_id={execution.execution_id}, error={e}",
|
||||
exc_info=True
|
||||
)
|
||||
self.update_execution_status(
|
||||
execution.execution_id,
|
||||
"failed",
|
||||
@@ -876,6 +884,72 @@ class WorkflowService:
|
||||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _validate_file_upload(
|
||||
features_config: dict[str, Any],
|
||||
files: Optional[list[FileInput]]
|
||||
) -> None:
|
||||
"""校验上传文件是否符合 file_upload 配置"""
|
||||
if not files:
|
||||
return
|
||||
fu = features_config.get("file_upload")
|
||||
if fu is None:
|
||||
return
|
||||
if not (isinstance(fu, dict) and fu.get("enabled")):
|
||||
raise BusinessException(
|
||||
"The application does not have file upload functionality enabled",
|
||||
BizCode.BAD_REQUEST
|
||||
)
|
||||
max_count = fu.get("max_file_count", 5)
|
||||
if len(files) > max_count:
|
||||
raise BusinessException(
|
||||
f"File count exceeds limit (maximum {max_count} files)",
|
||||
BizCode.BAD_REQUEST
|
||||
)
|
||||
|
||||
# 校验传输方式
|
||||
allowed_methods = fu.get("allowed_transfer_methods", ["local_file", "remote_url"])
|
||||
for f in files:
|
||||
if f.transfer_method.value not in allowed_methods:
|
||||
raise BusinessException(
|
||||
f"Unsupport file transfer method:{f.transfer_method.value},"
|
||||
f"allowed method:{', '.join(allowed_methods)}",
|
||||
BizCode.BAD_REQUEST
|
||||
)
|
||||
|
||||
# 各类型对应的开关和大小限制配置键
|
||||
type_cfg = {
|
||||
"image": ("image_enabled", "image_max_size_mb", 20, "image"),
|
||||
"audio": ("audio_enabled", "audio_max_size_mb", 50, "audio"),
|
||||
"document": ("document_enabled", "document_max_size_mb", 100, "document"),
|
||||
"video": ("video_enabled", "video_max_size_mb", 500, "video"),
|
||||
}
|
||||
|
||||
for f in files:
|
||||
ftype = str(f.type) # 如 "image", "audio", "document", "video"
|
||||
cfg = type_cfg.get(ftype)
|
||||
if cfg is None:
|
||||
continue
|
||||
enabled_key, size_key, default_max_mb, label = cfg
|
||||
|
||||
# 校验类型开关
|
||||
if not fu.get(enabled_key):
|
||||
raise BusinessException(
|
||||
f"The application has not enabled {label} file upload",
|
||||
BizCode.BAD_REQUEST
|
||||
)
|
||||
|
||||
# 校验文件大小(仅当内容已加载时)
|
||||
content = f.get_content()
|
||||
if content is not None:
|
||||
max_mb = fu.get(size_key, default_max_mb)
|
||||
size_mb = len(content) / (1024 * 1024)
|
||||
if size_mb > max_mb:
|
||||
raise BusinessException(
|
||||
f"{label} File size exceeds the limit (maximum {max_mb} MB, current {size_mb:.1f} MB)",
|
||||
BizCode.BAD_REQUEST
|
||||
)
|
||||
|
||||
|
||||
# ==================== 依赖注入函数 ====================
|
||||
|
||||
|
||||
Reference in New Issue
Block a user