refactor(agent, shipping): 简化代理模型并完善订单抽取业务
- 移除 Agent 的 exposure_mode 与 internal_executor 相关逻辑,清理关联的过滤、序列化代码 - 删除废弃的 AgentRuntimeBinding 模型及其依赖 - 新增订单抽取上传批次、请求快照等数据库迁移与业务逻辑 - 修复附件完整性检测、订单抽取状态映射问题 - 更新多语言错误文案与前端单元测试配置 - 清理平台配置中不再使用的暴露模式配置
This commit is contained in:
1 parent
1f3ecb87b7
commit
61ac6403c4
159 files changed
+12883
-5099
No files matched your search
@@ -14,8 +14,8 @@ import requests
|
||||
|
||||
from app import db
|
||||
from app.core.config.settings import Config
|
||||
from app.core.errors import ConflictError, NotFoundError, ValidationError
|
||||
from app.models import Agent, AgentRuntimeBinding, ShippingOrderExtractionRun, User
|
||||
from app.core.errors import NotFoundError, ValidationError
|
||||
from app.models import Agent, User
|
||||
from app.services.agent_directory import normalize_directory_member_ids, replace_directory_members
|
||||
from app.services.admin.training_exam_pdf_service import parse_training_question_bank_from_guide
|
||||
from app.services.agent_registry.admin_payload import apply_agent_payload, validate_agent_payload
|
||||
@@ -29,20 +29,9 @@ def serialize_agent(agent: Agent) -> dict:
|
||||
return serialize_agent_for_admin(agent)
|
||||
|
||||
|
||||
def list_agents(*, scope: str | None = None) -> list[dict]:
|
||||
"""按管理域返回 Agent;scope=applications/executors,缺省返回全部兼容旧客户端。"""
|
||||
query = db.session.query(Agent)
|
||||
if scope == 'applications':
|
||||
query = query.filter(Agent.exposure_mode == 'workspace')
|
||||
elif scope == 'executors':
|
||||
query = query.filter(Agent.exposure_mode == 'internal')
|
||||
elif scope not in (None, ''):
|
||||
raise ValidationError(
|
||||
'Invalid agent management scope',
|
||||
code='AGENT.ADMIN_SCOPE_INVALID',
|
||||
details={'scope': scope},
|
||||
)
|
||||
agents = query.order_by(Agent.id.asc()).all()
|
||||
def list_agents() -> list[dict]:
|
||||
"""返回全部 Agent(按 id 升序)。"""
|
||||
agents = db.session.query(Agent).order_by(Agent.id.asc()).all()
|
||||
return [serialize_agent(a) for a in agents]
|
||||
|
||||
|
||||
@@ -71,24 +60,6 @@ def create_agent(data: dict[str, Any]) -> dict:
|
||||
db.session.flush()
|
||||
member_ids = data.get('directory_member_ids') if agent.is_aggregate else []
|
||||
replace_directory_members(agent, normalize_directory_member_ids(member_ids, directory_agent_id=agent.id))
|
||||
runtime_binding = data.get('order_extraction_runtime_binding')
|
||||
if runtime_binding is not None:
|
||||
if not isinstance(runtime_binding, dict):
|
||||
raise ValidationError('order_extraction_runtime_binding 必须为 JSON 对象')
|
||||
try:
|
||||
provider_agent_id = int(runtime_binding.get('provider_agent_id'))
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValidationError('provider_agent_id is required') from exc
|
||||
from app.services.agent_runtime_binding_service import save_runtime_binding
|
||||
|
||||
save_runtime_binding(
|
||||
owner_agent_id=agent.id,
|
||||
capability_code='shipping.order_extraction',
|
||||
provider_agent_id=provider_agent_id,
|
||||
enabled=runtime_binding.get('enabled') is not False,
|
||||
config=runtime_binding.get('config'),
|
||||
commit=False,
|
||||
)
|
||||
db.session.commit()
|
||||
return {'id': agent.id}
|
||||
|
||||
@@ -115,7 +86,6 @@ def update_agent(agent_id: int, data: dict[str, Any]) -> None:
|
||||
'runtime_provider': data.get('runtime_provider', agent.runtime_provider),
|
||||
'conversation_mode': data.get('conversation_mode', agent.conversation_mode),
|
||||
'dify_app_type': data.get('dify_app_type', agent.dify_app_type),
|
||||
'exposure_mode': data.get('exposure_mode', agent.exposure_mode),
|
||||
'iframe_url': data.get('iframe_url', agent.iframe_url),
|
||||
'category': data.get('category', agent.category),
|
||||
'sub_category': data.get('sub_category', agent.sub_category),
|
||||
@@ -138,44 +108,13 @@ def update_agent(agent_id: int, data: dict[str, Any]) -> None:
|
||||
if 'directory_member_ids' in data or 'is_aggregate' in data:
|
||||
member_ids = data.get('directory_member_ids') if agent.is_aggregate else []
|
||||
replace_directory_members(agent, normalize_directory_member_ids(member_ids, directory_agent_id=agent.id))
|
||||
runtime_binding = data.get('order_extraction_runtime_binding')
|
||||
if runtime_binding is not None:
|
||||
if not isinstance(runtime_binding, dict):
|
||||
raise ValidationError('order_extraction_runtime_binding 必须为 JSON 对象')
|
||||
try:
|
||||
provider_agent_id = int(runtime_binding.get('provider_agent_id'))
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValidationError('provider_agent_id is required') from exc
|
||||
from app.services.agent_runtime_binding_service import save_runtime_binding
|
||||
|
||||
save_runtime_binding(
|
||||
owner_agent_id=agent.id,
|
||||
capability_code='shipping.order_extraction',
|
||||
provider_agent_id=provider_agent_id,
|
||||
enabled=runtime_binding.get('enabled') is not False,
|
||||
config=runtime_binding.get('config'),
|
||||
commit=False,
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def delete_agent(agent_id: int) -> None:
|
||||
"""删除 Agent;被能力绑定或历史订单抽取 Run 引用时明确阻断。"""
|
||||
"""删除 Agent(不存在时静默成功)。"""
|
||||
agent = db.session.get(Agent, agent_id)
|
||||
if agent:
|
||||
binding_count = AgentRuntimeBinding.query.filter(
|
||||
(AgentRuntimeBinding.owner_agent_id == agent.id)
|
||||
| (AgentRuntimeBinding.provider_agent_id == agent.id)
|
||||
).count()
|
||||
run_count = ShippingOrderExtractionRun.query.filter(
|
||||
(ShippingOrderExtractionRun.owner_agent_id == agent.id)
|
||||
).count()
|
||||
if binding_count or run_count:
|
||||
raise ConflictError(
|
||||
'Agent is referenced by runtime capability history',
|
||||
code='AGENT.RUNTIME_PROVIDER_DELETE_BLOCKED',
|
||||
details={'binding_count': binding_count, 'run_count': run_count},
|
||||
)
|
||||
db.session.delete(agent)
|
||||
db.session.commit()
|
||||
|
||||
@@ -356,11 +295,6 @@ def update_agent_permissions(agent_id: int, user_ids: list[int]) -> None:
|
||||
agent = db.session.get(Agent, agent_id)
|
||||
if not agent:
|
||||
raise NotFoundError('Not found')
|
||||
if agent.exposure_mode == 'internal' and user_ids:
|
||||
raise ValidationError(
|
||||
'内部执行器不可授予用户权限',
|
||||
code='AGENT.INTERNAL_EXECUTOR_PERMISSION_FORBIDDEN',
|
||||
)
|
||||
current_users = db.session.query(User).filter(User.agents.contains(agent)).all()
|
||||
current_user_ids = {u.id for u in current_users}
|
||||
new_user_ids = set(user_ids)
|
||||
|
||||
Reference in new issue
Block a user