"""Shared parser capability catalogue; installed services remain explicit resources.""" BACKENDS_2 = { 'pipeline': '传统管道', 'hybrid-auto-engine': '混合本地引擎', 'vlm-auto-engine': 'VLM 本地引擎', 'hybrid-http-client': '混合远程推理', 'vlm-http-client': 'VLM 远程推理', } BACKENDS_3 = { 'pipeline': '传统管道', 'hybrid-engine': '混合本地引擎', 'vlm-engine': 'VLM 本地引擎', 'hybrid-http-client': '混合远程推理', 'vlm-http-client': 'VLM 远程推理', } TIERS = {'flash': 'Flash 快速', 'basic': 'Basic 基础', 'standard': 'Standard 标准', 'advanced': 'Advanced 高精度'} def catalogue(): return {'versions': [{'id': '2.7.6', 'protocol': 'legacy', 'backends': BACKENDS_2}, {'id': '2.7.1', 'protocol': 'legacy', 'backends': BACKENDS_2}, {'id': '2.7.4', 'protocol': 'legacy', 'backends': BACKENDS_2}, {'id': '3.4.5', 'protocol': 'legacy', 'backends': BACKENDS_3}, {'id': '4.x', 'protocol': 'v1', 'backends': TIERS}], 'builtin': {'id': 'builtin', 'name': '内置解析', 'backend': 'builtin', 'formats': ['PDF', 'DOCX', 'DOC', 'XLSX', 'XLS', 'CSV', 'HTML', 'MD', 'TXT']}} def validate_options(options, endpoint): from urllib.parse import urlsplit from rest_framework.exceptions import ValidationError if not isinstance(options, dict) or set(options) - {'backend', 'language', 'ocr', 'formula', 'table', 'protocol', 'tier', 'engine_version', 'server_url', 'effort'}: raise ValidationError('解析参数无效') version = options.get('engine_version', '4.x' if options.get('protocol') == 'v1' else '2.7.1') spec = next((v for v in catalogue()['versions'] if v['id'] == version), None) if not spec or options.get('protocol', spec['protocol']) != spec['protocol']: raise ValidationError('解析版本与接口不匹配') if spec['protocol'] == 'legacy': if urlsplit(endpoint).path.endswith('/v1'): raise ValidationError('2.x / 3.x 请填写解析 API 地址,不含 /v1') backend = options.get('backend', 'pipeline') if not isinstance(backend, str): raise ValidationError('解析后端无效') aliases = {'vlm-auto-engine': 'vlm-engine', 'hybrid-auto-engine': 'hybrid-engine'} if version == '3.4.5' else {} if aliases.get(backend, backend) not in spec['backends']: raise ValidationError('该版本不支持此解析后端') if backend.endswith('-http-client'): if not isinstance(options.get('server_url'), str): raise ValidationError('请填写有效的远程推理地址') remote = urlsplit(options.get('server_url', '')) if remote.scheme not in ('http', 'https') or not remote.hostname or remote.username or remote.password or remote.query or remote.fragment: raise ValidationError('请填写有效的远程推理地址') elif options.get('tier', 'standard') not in TIERS: raise ValidationError('解析档位无效') if options.get('effort', 'medium') not in ('medium', 'high'): raise ValidationError('解析精度无效') if options.get('language', 'ch') not in ('ch', 'en', 'japan', 'korean', 'chinese_cht'): raise ValidationError('识别语言无效') if any(not isinstance(options[k], bool) for k in ('ocr', 'formula', 'table') if k in options): raise ValidationError('解析参数无效') return {**options, 'engine_version': version, 'protocol': spec['protocol']}