feat(shipping-notice): 添加订单抽取功能,支持异步运行和管理,优化相关数据模型和处理逻辑
This commit is contained in:
1 parent
34d489a2d1
commit
ce26d6d0c4
144 files changed
+12241
-4233
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 NotFoundError, ValidationError
|
||||
from app.models import Agent, User
|
||||
from app.core.errors import ConflictError, NotFoundError, ValidationError
|
||||
from app.models import Agent, AgentRuntimeBinding, ShippingOrderExtractionRun, 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
|
||||
@@ -60,6 +60,24 @@ 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}
|
||||
|
||||
@@ -86,6 +104,7 @@ 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),
|
||||
@@ -108,13 +127,45 @@ 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(不存在时静默成功)。"""
|
||||
"""删除 Agent;被能力绑定或历史订单抽取 Run 引用时明确阻断。"""
|
||||
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)
|
||||
| (ShippingOrderExtractionRun.provider_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()
|
||||
|
||||
|
||||
Reference in new issue
Block a user