Files
company-ai-platform/app/modules/work_assistant/bid_studio_partial.py
T
2026-11-27 14:43:37 +08:00

81 lines
4.0 KiB
Python

"""Deliver usable analysis independently of optional organization/audit failures."""
from copy import deepcopy
def rows(value, key):
found = value.get(key) if isinstance(value, dict) else None
return found if isinstance(found, list) else []
def partial_analysis(request, candidate, progress):
from .bid_studio import CATEGORIES, Finding, SummaryItem, ExcludedEvidence, normalize_summary, normalize_invalidation_sections
context = request.workbench
phase = context.get('phase')
if phase not in {'analysis', 'summary'}:
return None
message = '已保留可识别的内容,部分条目尚待核对,可继续生成目录。'
if phase == 'analysis':
source_ids = {block.id for block in request.blocks}
findings = []
raw = candidate.get('raw', {})
for row in rows(raw, 'findings'):
try:
item = Finding.model_validate(row).model_dump()
except (ValueError, TypeError):
continue
if item['category'] in CATEGORIES and set(item['source_ids']) <= source_ids:
findings.append(item)
# A malformed batch must not replace the project name or invent findings.
result = {'project_name': '', 'findings': findings}
if not findings:
message = '本批未获得可用解析,其他已识别内容已保留;可补充核对原文后继续。'
else:
category = context.get('category')
if category not in CATEGORIES:
return None
evidence = {row['id']: row for row in context.get('evidence', []) if isinstance(row, dict) and row.get('id')}
raw = candidate.get('organized') or candidate.get('raw') or (progress.state.get('studio_summary_base') if progress else {}) or {}
sections, excluded = [], []
for section in rows(raw, 'sections'):
if not isinstance(section, dict) or not isinstance(section.get('items'), list):
continue
items = []
for row in section['items']:
try:
item = SummaryItem.model_validate(row).model_dump()
except (ValueError, TypeError):
continue
# Never accept unsupported text by merely deleting its bad citation.
if set(item['evidence_ids']) <= evidence.keys():
items.append(item)
if items:
sections.append({'heading': str(section.get('heading') or '已识别条目'), 'items': items})
for row in rows(raw, 'excluded'):
try:
item = ExcludedEvidence.model_validate(row).model_dump()
except (ValueError, TypeError):
continue
if item['evidence_id'] in evidence:
excluded.append(item)
covered = {key for section in sections for item in section['items'] for key in item['evidence_ids']}
covered.update(row['evidence_id'] for row in excluded)
# Source extraction already succeeded. Keep its facts if organization did
# not, instead of losing the entire category or making another model call.
remaining = [{'text': row['text'], 'level': 0, 'evidence_ids': [key]}
for key, row in evidence.items() if key not in covered
and isinstance(row.get('text'), str) and row['text'].strip()]
if remaining:
sections.append({'heading': '已识别条目(待核对)', 'items': remaining})
result = {'sections': sections, 'excluded': excluded}
normalize_summary(result)
if category == 'disqualification':
normalize_invalidation_sections(result)
if not sections and not excluded:
message = '本项暂未获得可用解析,可继续使用其他已识别内容;需要时可手动补充。'
result.update(partial=True, warnings=[message])
if progress:
progress.state['studio_partial_result'] = deepcopy(result)
progress.state.pop('workbench_failure', None)
progress.persist(True)
return result