refactor(agent, shipping): 简化代理模型并完善订单抽取业务

- 移除 Agent 的 exposure_mode 与 internal_executor 相关逻辑,清理关联的过滤、序列化代码
- 删除废弃的 AgentRuntimeBinding 模型及其依赖
- 新增订单抽取上传批次、请求快照等数据库迁移与业务逻辑
- 修复附件完整性检测、订单抽取状态映射问题
- 更新多语言错误文案与前端单元测试配置
- 清理平台配置中不再使用的暴露模式配置
This commit is contained in:
hugo committed 2026-07-28 20:25:48 +08:00
1 parent 1f3ecb87b7
commit 61ac6403c4
159 files changed
+12883 -5099

No files matched your search

@@ -7,7 +7,7 @@ import pytest
from app import db
from app.core.errors import NotFoundError, ValidationError
from app.models import Agent, AgentRuntimeBinding, User
from app.models import Agent, User
from app.services.admin import agent_service
@@ -85,87 +85,6 @@ def test_update_agent_clears_handler_config_when_explicit_null(app):
assert refreshed.handler_config is None
def test_create_agent_and_runtime_binding_are_atomic(app):
"""创建货代 Agent 时绑定校验失败必须回滚 Agent,不能留下半配置入口。"""
with app.app_context():
provider = Agent(
name='Invalid Chatflow Provider',
agent_code='generic',
runtime_provider='dify',
dify_app_type='chatflow',
exposure_mode='workspace',
dify_app_id='invalid-create-provider',
dify_api_key='sk-test',
api_base_url='http://dify.example/v1',
is_active=True,
)
db.session.add(provider)
db.session.commit()
with pytest.raises(ValidationError):
agent_service.create_agent({
'name': 'Atomic Shipping Create',
'agent_code': 'shipping_notice',
'runtime_provider': 'hybrid',
'conversation_mode': 'task_chat',
'dify_app_id': 'atomic-shipping-create',
'dify_api_key': 'sk-test',
'api_base_url': 'http://dify.example/v1',
'order_extraction_runtime_binding': {
'provider_agent_id': provider.id,
'enabled': True,
'config': {'timeout_seconds': 900, 'max_attempts': 3},
},
})
db.session.rollback()
assert Agent.query.filter_by(name='Atomic Shipping Create').count() == 0
assert AgentRuntimeBinding.query.count() == 0
def test_update_agent_and_runtime_binding_are_atomic(app):
"""更新 Agent 时绑定校验失败必须回滚 Agent 字段,避免配置只保存一半。"""
with app.app_context():
owner = Agent(
name='Atomic Shipping Update',
description='before',
agent_code='shipping_notice',
runtime_provider='hybrid',
conversation_mode='task_chat',
dify_app_id='atomic-shipping-update',
dify_api_key='sk-test',
api_base_url='http://dify.example/v1',
is_active=True,
)
provider = Agent(
name='Invalid Update Provider',
agent_code='generic',
runtime_provider='dify',
dify_app_type='chatflow',
exposure_mode='workspace',
dify_app_id='invalid-update-provider',
dify_api_key='sk-test',
api_base_url='http://dify.example/v1',
is_active=True,
)
db.session.add_all([owner, provider])
db.session.commit()
with pytest.raises(ValidationError):
agent_service.update_agent(owner.id, {
'description': 'after',
'order_extraction_runtime_binding': {
'provider_agent_id': provider.id,
'enabled': True,
'config': {'timeout_seconds': 900, 'max_attempts': 3},
},
})
db.session.rollback()
assert db.session.get(Agent, owner.id).description == 'before'
assert AgentRuntimeBinding.query.filter_by(owner_agent_id=owner.id).count() == 0
def test_update_agent_permissions(app):
with app.app_context():
agent = Agent(
@@ -50,31 +50,6 @@ def test_batch_update_permissions_no_users(app):
permission_service.batch_update_permissions({'user_ids': [], 'agent_ids': [1]})
@pytest.mark.parametrize('operation', ['single', 'batch'])
def test_internal_executor_cannot_be_assigned_to_user(app, operation):
"""授权服务必须在所有写入口拒绝 internal 执行器。"""
with app.app_context():
executor = Agent(
name='internal-executor', type='api', dify_app_id='wf-internal', dify_api_key='key',
dify_app_type='workflow', runtime_provider='dify', exposure_mode='internal',
)
user = User(username=f'internal-{operation}', email=f'internal-{operation}@example.com', role='user')
db.session.add_all([executor, user])
db.session.commit()
with pytest.raises(ValidationError) as caught:
if operation == 'single':
permission_service.update_user_permissions(user.id, {'agent_ids': [executor.id]})
else:
permission_service.batch_update_permissions({
'user_ids': [user.id], 'agent_ids': [executor.id], 'action': 'add',
})
assert caught.value.code == 'AGENT.INTERNAL_EXECUTOR_USER_ACCESS_FORBIDDEN'
db.session.refresh(user)
assert executor not in user.agents
def test_permission_request_approve(app, monkeypatch):
with app.app_context():
agent = Agent(name='req-agent', type='api', dify_app_id='app-r', dify_api_key='key-r')
@@ -100,27 +75,3 @@ def test_permission_request_approve(app, monkeypatch):
def test_permission_request_not_found(app):
with app.app_context():
with pytest.raises(NotFoundError):
permission_service.approve_permission_request(99999)
def test_internal_executor_permission_request_cannot_be_approved(app):
"""历史或伪造的内部执行器权限申请不得转化为用户授权。"""
with app.app_context():
executor = Agent(
name='request-executor', type='api', dify_app_id='wf-request', dify_api_key='key',
dify_app_type='workflow', runtime_provider='dify', exposure_mode='internal',
)
user = User(username='request-internal', email='request-internal@example.com', role='user')
db.session.add_all([executor, user])
db.session.flush()
req = PermissionRequest(user_id=user.id, agent_id=executor.id, reason='invalid', status='pending')
db.session.add(req)
db.session.commit()
with pytest.raises(ForbiddenError) as caught:
permission_service.approve_permission_request(req.id)
assert getattr(caught.value, 'code', None) == 'AGENT.INTERNAL_EXECUTOR_USER_ACCESS_FORBIDDEN'
db.session.refresh(user)
assert executor not in user.agents
@@ -54,23 +54,3 @@ def test_list_users_sort_by_username_and_department(app):
assert depts == sorted(depts, reverse=True)
fallback = user_service.resolve_users_order_by({'sort_by': 'role', 'sort_dir': 'asc'})
assert len(fallback) == 1
def test_update_user_rejects_internal_executor_permission(app):
"""用户 CRUD 的 agent_ids 写入口不得绕过统一授权策略。"""
with app.app_context():
user = User(username='crud-internal', email='crud-internal@example.com', role='user')
executor = Agent(
name='CRUD Executor', exposure_mode='internal', runtime_provider='dify',
dify_app_type='workflow', dify_app_id='crud-workflow', dify_api_key='key',
)
db.session.add_all([user, executor])
db.session.commit()
with pytest.raises(ValidationError) as caught:
user_service.update_user(user.id, {'agent_ids': [executor.id]})
assert caught.value.code == 'AGENT.INTERNAL_EXECUTOR_USER_ACCESS_FORBIDDEN'
db.session.refresh(user)
assert executor not in user.agents
@@ -21,28 +21,3 @@ def test_binding_stats(app):
stats = wecom_admin_service.binding_stats()
assert stats['total_active'] == 2
assert stats['bound'] == 1
assert stats['unbound'] == 1
def test_provision_user_rejects_internal_executor_permission(app):
"""企微开户/绑定不得把内部执行器写入用户授权关系。"""
with app.app_context():
user = User(username='wecom-internal', email='wecom-internal@example.com', role='user')
executor = Agent(
name='WeCom Executor', exposure_mode='internal', runtime_provider='dify',
dify_app_type='workflow', dify_app_id='wecom-workflow', dify_api_key='key',
)
db.session.add_all([user, executor])
db.session.commit()
with pytest.raises(ValidationError) as caught:
wecom_admin_service.provision_user({
'create_user': False,
'user_id': user.id,
'bind': {'wecom_userid': 'wx-internal'},
'agent_ids': [executor.id],
})
assert caught.value.code == 'AGENT.INTERNAL_EXECUTOR_USER_ACCESS_FORBIDDEN'
db.session.refresh(user)
assert executor not in user.agents