feat(shipping-notice): 添加 MinerU 订单附件表格结构化功能,更新环境配置和数据模型以支持新布局解析和文档模板管理
This commit is contained in:
1 parent
c4095b08da
commit
c35223d49d
148 files changed
+15709
-1353
No files matched your search
@@ -17,6 +17,13 @@ from app.services.agent_registry.catalog import get_catalog_entry
|
||||
from app.services.agent_registry.resolver import resolve_effective_role
|
||||
|
||||
|
||||
_EXECUTOR_GOVERNANCE_DEFAULTS = {
|
||||
'provider_max_concurrency': 4,
|
||||
'circuit_failure_threshold': 5,
|
||||
'circuit_cooldown_seconds': 60,
|
||||
}
|
||||
|
||||
|
||||
def _capability_definition(owner: Agent, capability_code: str) -> dict[str, Any]:
|
||||
"""读取 owner Agent Catalog 中声明的能力定义;未声明能力不可由管理员任意绑定。"""
|
||||
entry = get_catalog_entry(owner.agent_code) or {}
|
||||
@@ -37,13 +44,7 @@ def _normalize_config(raw: Any) -> dict[str, Any]:
|
||||
raw = {}
|
||||
if not isinstance(raw, dict):
|
||||
raise ValidationError('config must be an object', code='AGENT.RUNTIME_BINDING_CONFIG_INVALID')
|
||||
unknown = set(raw) - {
|
||||
'timeout_seconds', 'max_attempts', 'unit_concurrency',
|
||||
'unit_max_attempts', 'unit_retry_backoff_ms',
|
||||
'max_model_units', 'provider_max_concurrency',
|
||||
'circuit_failure_threshold', 'circuit_cooldown_seconds',
|
||||
'auto_create_enabled',
|
||||
}
|
||||
unknown = set(raw) - {'timeout_seconds', 'max_attempts', 'auto_create_enabled'}
|
||||
if unknown:
|
||||
raise ValidationError(
|
||||
'Unsupported runtime binding config keys',
|
||||
@@ -53,14 +54,6 @@ def _normalize_config(raw: Any) -> dict[str, Any]:
|
||||
try:
|
||||
timeout_seconds = int(raw.get('timeout_seconds', 900))
|
||||
max_attempts = int(raw.get('max_attempts', 3))
|
||||
unit_concurrency = int(raw.get('unit_concurrency', 2))
|
||||
unit_max_attempts = int(raw.get('unit_max_attempts', 2))
|
||||
unit_retry_backoff_ms = int(raw.get('unit_retry_backoff_ms', 500))
|
||||
max_model_units = int(raw.get('max_model_units', 20))
|
||||
provider_max_concurrency = int(raw.get('provider_max_concurrency', 4))
|
||||
circuit_failure_threshold = int(raw.get('circuit_failure_threshold', 5))
|
||||
circuit_cooldown_seconds = int(raw.get('circuit_cooldown_seconds', 60))
|
||||
auto_create_enabled = raw.get('auto_create_enabled', False)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValidationError(
|
||||
'Runtime binding execution settings must be integers',
|
||||
@@ -71,34 +64,34 @@ def _normalize_config(raw: Any) -> dict[str, Any]:
|
||||
'Runtime binding config is outside allowed limits',
|
||||
code='AGENT.RUNTIME_BINDING_CONFIG_INVALID',
|
||||
)
|
||||
if not 2 <= unit_concurrency <= 4 or not 1 <= unit_max_attempts <= 3:
|
||||
raise ValidationError(
|
||||
'Runtime binding unit concurrency/attempts are outside allowed limits',
|
||||
code='AGENT.RUNTIME_BINDING_CONFIG_INVALID',
|
||||
)
|
||||
if not 100 <= unit_retry_backoff_ms <= 5000:
|
||||
raise ValidationError(
|
||||
'Runtime binding unit retry backoff is outside allowed limits',
|
||||
code='AGENT.RUNTIME_BINDING_CONFIG_INVALID',
|
||||
)
|
||||
if not 1 <= max_model_units <= 100 or not 1 <= provider_max_concurrency <= 20:
|
||||
raise ValidationError('Runtime binding provider budget is outside allowed limits', code='AGENT.RUNTIME_BINDING_CONFIG_INVALID')
|
||||
if not 2 <= circuit_failure_threshold <= 20 or not 10 <= circuit_cooldown_seconds <= 600:
|
||||
raise ValidationError('Runtime binding circuit settings are outside allowed limits', code='AGENT.RUNTIME_BINDING_CONFIG_INVALID')
|
||||
if not isinstance(auto_create_enabled, bool):
|
||||
raise ValidationError('Runtime binding auto-create gate must be boolean', code='AGENT.RUNTIME_BINDING_CONFIG_INVALID')
|
||||
return {
|
||||
result = {
|
||||
'timeout_seconds': timeout_seconds,
|
||||
'max_attempts': max_attempts,
|
||||
'unit_concurrency': unit_concurrency,
|
||||
'unit_max_attempts': unit_max_attempts,
|
||||
'unit_retry_backoff_ms': unit_retry_backoff_ms,
|
||||
'max_model_units': max_model_units,
|
||||
'provider_max_concurrency': provider_max_concurrency,
|
||||
'circuit_failure_threshold': circuit_failure_threshold,
|
||||
'circuit_cooldown_seconds': circuit_cooldown_seconds,
|
||||
'auto_create_enabled': auto_create_enabled,
|
||||
}
|
||||
if 'auto_create_enabled' in raw:
|
||||
if not isinstance(raw['auto_create_enabled'], bool):
|
||||
raise ValidationError('auto_create_enabled must be boolean', code='AGENT.RUNTIME_BINDING_CONFIG_INVALID')
|
||||
result['auto_create_enabled'] = raw['auto_create_enabled']
|
||||
return result
|
||||
|
||||
|
||||
def resolve_executor_governance(provider: Agent) -> dict[str, int]:
|
||||
"""读取内部执行器全局治理,供新 Run 冻结并避免入口应用绕过 Provider 保护。"""
|
||||
raw_extra = provider.extra_config if isinstance(provider.extra_config, dict) else {}
|
||||
raw = raw_extra.get('order_extraction_governance') if isinstance(raw_extra.get('order_extraction_governance'), dict) else {}
|
||||
try:
|
||||
config = {
|
||||
'provider_max_concurrency': int(raw.get('provider_max_concurrency', _EXECUTOR_GOVERNANCE_DEFAULTS['provider_max_concurrency'])),
|
||||
'circuit_failure_threshold': int(raw.get('circuit_failure_threshold', _EXECUTOR_GOVERNANCE_DEFAULTS['circuit_failure_threshold'])),
|
||||
'circuit_cooldown_seconds': int(raw.get('circuit_cooldown_seconds', _EXECUTOR_GOVERNANCE_DEFAULTS['circuit_cooldown_seconds'])),
|
||||
}
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValidationError('Internal executor governance settings must be integers', code='AGENT.EXECUTOR_GOVERNANCE_INVALID') from exc
|
||||
if not 1 <= config['provider_max_concurrency'] <= 20:
|
||||
raise ValidationError('Internal executor concurrency is outside allowed limits', code='AGENT.EXECUTOR_GOVERNANCE_INVALID')
|
||||
if not 2 <= config['circuit_failure_threshold'] <= 20 or not 10 <= config['circuit_cooldown_seconds'] <= 600:
|
||||
raise ValidationError('Internal executor circuit settings are outside allowed limits', code='AGENT.EXECUTOR_GOVERNANCE_INVALID')
|
||||
return config
|
||||
|
||||
|
||||
def _validate_provider(provider: Agent, definition: dict[str, Any]) -> None:
|
||||
@@ -145,6 +138,7 @@ def serialize_runtime_binding(binding: AgentRuntimeBinding) -> dict[str, Any]:
|
||||
'is_active': provider.is_active,
|
||||
} if provider else None,
|
||||
'enabled': bool(binding.enabled),
|
||||
'quality_batch_id': binding.quality_batch_id,
|
||||
'config': dict(binding.config_json or {}),
|
||||
}
|
||||
|
||||
@@ -166,10 +160,11 @@ def save_runtime_binding(
|
||||
capability_code: str,
|
||||
provider_agent_id: int,
|
||||
enabled: bool,
|
||||
quality_batch_id: int | None = None,
|
||||
config: Any,
|
||||
commit: bool = True,
|
||||
) -> AgentRuntimeBinding:
|
||||
"""创建或更新能力绑定;owner/provider 必须是不同 Agent 且满足 Catalog 约束。"""
|
||||
"""创建或更新能力绑定;开启自动建单时必须引用已批准质量批次。"""
|
||||
owner = db.session.get(Agent, owner_agent_id)
|
||||
provider = db.session.get(Agent, provider_agent_id)
|
||||
if owner is None or provider is None:
|
||||
@@ -181,6 +176,15 @@ def save_runtime_binding(
|
||||
if enabled:
|
||||
_validate_provider(provider, definition)
|
||||
normalized_config = _normalize_config(config)
|
||||
if code == 'shipping.order_extraction' and normalized_config.get('auto_create_enabled'):
|
||||
from app.services.shipping_notice.order_extraction_quality_service import require_approved_quality_batch
|
||||
from app.services.shipping_notice.order_schema.runtime import published_version
|
||||
|
||||
require_approved_quality_batch(
|
||||
batch_id=quality_batch_id,
|
||||
owner_agent_id=owner.id,
|
||||
schema_version_id=int(published_version().id),
|
||||
)
|
||||
binding = AgentRuntimeBinding.query.filter_by(
|
||||
owner_agent_id=owner.id, capability_code=code,
|
||||
).first()
|
||||
@@ -189,6 +193,7 @@ def save_runtime_binding(
|
||||
db.session.add(binding)
|
||||
binding.provider_agent_id = provider.id
|
||||
binding.enabled = bool(enabled)
|
||||
binding.quality_batch_id = int(quality_batch_id) if quality_batch_id is not None else None
|
||||
binding.config_json = normalized_config
|
||||
if commit:
|
||||
db.session.commit()
|
||||
|
||||
Reference in new issue
Block a user