sandbox_agent_helpers.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """离线模型动作脚本:真实文件工具、真实沙箱和真实提交,替代旧 MainFanResult 假完成。"""
  2. import json
  3. import uuid
  4. from langchain_core.messages import AIMessage
  5. from db import db_conn, main_fan_store
  6. def tool_call(name, args):
  7. return AIMessage(content='', tool_calls=[{'name': name, 'args': args, 'id': uuid.uuid4().hex}],
  8. usage_metadata={'input_tokens': 20, 'output_tokens': 5, 'total_tokens': 25})
  9. def sandbox_messages(user, data, *, encoded_lists=False):
  10. yield tool_call('read_file', {'file_path': '/skills/main-fan-equivalent-capability-assessment/SKILL.md'})
  11. yield tool_call('prepare_main_fan_task', {'target': data})
  12. row = db_conn.query_one('SELECT id,session_id FROM main_fan_workflows WHERE user_name=? ORDER BY created_at DESC LIMIT 1', (user,))
  13. assert row, '准备工具没有创建任务'
  14. workflow_id, session = row['id'], row['session_id']
  15. code = f'''
  16. from pathlib import Path
  17. import json
  18. raw = json.loads(Path('exploration/{workflow_id}/input.json').read_text())
  19. metrics = {{p['metric']: p for p in raw.get('points', [])}}
  20. value = None
  21. if all(key in metrics for key in ('flow', 'static_pressure', 'shaft_power')):
  22. value = metrics['flow']['value'] * metrics['static_pressure']['value'] / (10 * metrics['shaft_power']['value'])
  23. summary = '已根据本轮数据执行计算,三项对照尚不完整。' if value is not None else '本轮没有可比资料。'
  24. body = '# 自编报告\\n\\n' + summary + ('\\n\\n静压效率:' + str(value) + '%' if value is not None else '')
  25. Path('agent-report.md').write_text(body)
  26. assessment = {{'status': 'completed' if value is not None else 'needs_materials',
  27. 'result_code': 'UNDETERMINED', 'summary': summary,
  28. 'evidence': [{{'source': '/workspace/exploration/{workflow_id}/input.json', 'pointer': '/points'}}],
  29. 'details': {{'efficiency_pct': value}}}}
  30. Path('agent-result.json').write_text(json.dumps(assessment, ensure_ascii=False))
  31. print('actual sandbox delivery')
  32. '''
  33. yield tool_call('write_file', {'file_path': '/workspace/agent-analysis.py', 'content': code})
  34. output_paths = ['agent-report.md', 'agent-result.json'] if encoded_lists else ['/workspace/agent-report.md', '/workspace/agent-result.json']
  35. yield tool_call('run_python', {'workflow_id': workflow_id,
  36. 'script_path': 'agent-analysis.py' if encoded_lists else '/workspace/agent-analysis.py',
  37. 'output_paths': json.dumps(output_paths) if encoded_lists else output_paths})
  38. workflow = main_fan_store.workflow(workflow_id, user, session)
  39. runs = workflow['context']['python_runs']
  40. assert runs and runs[-1]['exit_code'] == 0, runs
  41. yield tool_call('submit_main_fan_report', {'workflow_id': workflow_id,
  42. 'report_path': '/workspace/agent-report.md', 'calculations_path': '/workspace/agent-result.json',
  43. 'python_run_ids': json.dumps([runs[-1]['run_id']]) if encoded_lists else [runs[-1]['run_id']]})