71 lines
4.2 KiB
Python
71 lines
4.2 KiB
Python
"""Authorized workbench entry for outline inspection, rules, manual edits and model jobs."""
|
|
from rest_framework.exceptions import ValidationError
|
|
from system_manage import ai_hub as hub
|
|
from system_manage.heading_structure import analyze, artifact_state, manual_edit, validate_manual
|
|
|
|
|
|
def invoke_model(workspace, user, node, markdown, source, parent, progress):
|
|
"""Optional title assistance must not discard successful document parsing."""
|
|
from system_manage import mcp_runtime
|
|
output, report = analyze(markdown, source, source.get('heading_structure'))
|
|
if not report['uncertain_count']:
|
|
report['model'] = {'status': 'skipped', 'reason': '现有依据充分,无需模型校正'}
|
|
return {'text': output, 'structured': {'heading_structure': report}}
|
|
try:
|
|
model = hub.model_for(workspace, user, node['model_id'])
|
|
if model.update_time.isoformat() != node['resource_version']:
|
|
raise ValueError('model changed')
|
|
params = {'text': markdown, 'operation': 'heading_repair',
|
|
'heading_structure': source.get('heading_structure', {}), 'heading_source': source,
|
|
'temperature': 0, **{key: node[key] for key in ('context_tokens', 'safety_tokens') if key in node}}
|
|
if node.get('output_tokens') is not None:
|
|
params['max_tokens'] = node['output_tokens']
|
|
return mcp_runtime.invoke(workspace, user, 'model:' + str(model.id), params,
|
|
parent=parent, timeout=node.get('timeout', 180), progress=progress, scope={'source': 'mindmap'})
|
|
except Exception:
|
|
output, report = analyze(markdown, source, source.get('heading_structure'))
|
|
report['model'] = {'status': 'fallback', 'reason': '模型暂不可用,已保留解析和规则结果'}
|
|
return {'text': output, 'structured': {'heading_structure': report}}
|
|
|
|
|
|
def process(workspace, user, data, request=None):
|
|
markdown = hub.text(data.get('markdown'), '文档内容')
|
|
source, previous, original = {}, {}, markdown
|
|
if data.get('artifact_id'):
|
|
artifact = hub.one('artifacts', data['artifact_id'], workspace, user, edit=True)
|
|
previous, original = artifact_state(artifact), artifact.original
|
|
if artifact.task_id:
|
|
source = artifact.task.result.get('source') or {}
|
|
elif data.get('task_id'):
|
|
task = hub.one('tasks', data['task_id'], workspace, user)
|
|
if task.status != 'SUCCESS':
|
|
raise ValidationError('解析任务尚未完成')
|
|
source = task.result.get('source') or {}
|
|
previous, original = source.get('heading_structure', {}), task.result.get('original', markdown)
|
|
incoming = data.get('heading_structure')
|
|
if incoming is not None and not isinstance(incoming, dict):
|
|
raise ValidationError('层级数据无效')
|
|
mode = data.get('mode', 'inspect')
|
|
if mode not in ('inspect', 'rules', 'manual', 'model'):
|
|
raise ValidationError('层级操作无效')
|
|
try:
|
|
# Current line positions win; IDs/evidence/manual state come from the prior report.
|
|
_, report = analyze(markdown, source, previous or incoming, repair=False)
|
|
if incoming is not None:
|
|
report = validate_manual(report, incoming)
|
|
output, report = analyze(markdown, source, report, repair=mode in ('rules', 'model'))
|
|
if mode == 'manual':
|
|
action = data.get('action')
|
|
if not isinstance(action, dict):
|
|
raise ValueError('请选择层级操作')
|
|
output, report = manual_edit(output, report, action)
|
|
except (ValueError, TypeError) as exc:
|
|
raise ValidationError(str(exc) if isinstance(exc, ValueError) else '层级数据无效') from None
|
|
if mode != 'model':
|
|
return {'markdown': output, 'heading_structure': report, 'original': original}
|
|
task = hub.submit(workspace, user, {'name': '标题层级校正', 'markdown': output,
|
|
'original': original, 'heading_structure': report, '_heading_source': source,
|
|
'nodes': [{'type': 'organize', 'operation': 'heading_repair', 'model_id': data.get('model_id'),
|
|
'temperature': 0, 'prompt': '仅校正标题层级,保留正文。'}, {'type': 'render'}]}, request=request)
|
|
return {'task_id': str(task.id), 'status': task.status}
|