Files
2026-09-24 10:40:57 +08:00

74 lines
4.3 KiB
Python

"""Public task outcomes. Full exceptions belong in saved diagnostics, not chat."""
import json
import re
from pathlib import Path
from engine.validation import safe_name
def outcome(reason):
reason = str(reason or '')
if 'ReferenceUnavailable' in reason:
return 'needs_clarification', '暂未查到完整的配合资料,已保存进度。请补充下列尺寸或官方图纸后继续。'
if 'NeedsClarification' in reason:
return 'needs_clarification', '有一项要求会影响零件结构,请选择下面的方案,也可以输入自己的要求。'
if 'CADMemoryLimit' in reason or 'CADWorkerExit' in reason:
return 'paused', '建模运算异常中断,请从已保存的检查点继续。'
if 'BudgetExceeded' in reason or 'CADWorkerTimeout' in reason:
return 'paused', '本轮建模时间或调用额度已用完,尚有结构未完成或未核对。可以继续按原要求完成,也可以补充调整方案。'
if any(x in reason for x in ('NoProgress', 'RepairLimit')):
return 'paused', '程序在同一处建模或核对问题上重复尝试,尚未解决。可以继续修复;若想改变设计,也可以补充要求。'
if 'Cancelled' in reason:
return 'cancelled', '已停止生成,已完成的进度已保存。'
if any(x in reason for x in ('Protocol', 'ValidationError', 'max_output_tokens', 'incomplete')):
return 'paused', '本次建模或核对未能完成,已保留可用进度,可以稍后继续。'
if any(x in reason for x in ('Connection', 'Timeout', 'RateLimit', 'APIStatus', '余额', 'quota', 'credit')):
return 'unavailable', '生成服务暂时不可用,已保留进度。请稍后重试,持续出现时请联系管理员。'
return 'paused', '生成流程遇到内部异常,尚未完成。可以重试原任务;原始描述无需重新输入。'
def questions(base_dir, job_id):
if not job_id:
return []
try:
data = json.loads((Path(base_dir)/'output'/safe_name(job_id)/'clarification.json').read_text())
values = data.get('questions') or [v['question'] for v in data.get('items', [])]
return [str(v) for v in values if str(v).strip()]
except (OSError, ValueError, KeyError, TypeError):
return []
def failure_response(reason, base_dir, job_id=None):
status, message = outcome(reason)
folder = Path(base_dir)/'output'/safe_name(job_id) if job_id else None
if folder and not (folder/'model.step').exists() and ('CADMemoryLimit' in str(reason) or 'CADWorkerExit' in str(reason)):
message = '建模运算异常中断,原始需求和操作记录已保存,尚无可展示的实体。'
asks = questions(base_dir, job_id)
if asks:
status, message = 'needs_clarification', '下面的选择会影响零件结构。请选择方案或输入自己的要求,回答后继续原任务。'
elif status == 'needs_clarification':
status, message = 'paused', '程序未能整理出具体待确认问题。请重试原任务,无需重新输入需求。'
return {'ok': False, 'error': message, 'message': message, 'status': status,
'part_name': job_id, 'diagnostic_id': job_id, 'questions': asks,
'question_items': question_items(base_dir,job_id),
'can_resume': bool(job_id) and not asks, 'can_clarify': bool(asks),
'next_action': 'answer_questions' if asks else 'resume' if job_id else 'retry',
'has_model': bool(folder and (folder/'model.stl').exists())}
def question_items(base_dir, job_id):
if not job_id: return []
try:
from engine.quality_models import ClarificationQuestion
data=json.loads((Path(base_dir)/'output'/safe_name(job_id)/'clarification.json').read_text())
items=data.get('items',[])
if items: return [ClarificationQuestion.model_validate(v).model_dump() for v in items]
except (OSError,ValueError,KeyError,TypeError): pass
return [{'question':q,'reason':'','options':[]} for q in questions(base_dir,job_id)]
def issue_text(value):
text = str(value)
if re.search(r'Traceback|\w+(?:Error|Exception):|BudgetExceeded|RepairLimit|NoProgress|INVALID_|UNKNOWN_|POSTCONDITION|EXECUTION_ERROR|type=|https?://|unsupported operand', text):
return '此项尚未完成核对,具体诊断已保存。'
return text