- 新增步骤6任务模板(config/agent_submit_template.txt) - 步骤6调用智能体hz4th_editor执行提交操作 - 智能体根据产品类别选择对应API接口提交数据 - 页面新增步骤6模板编辑面板,支持查看/编辑/保存/预览 - 修正步骤5标题为'填充字段'(去掉'并提交') - 新增API: GET/POST /api/process/submit-template
422 lines
13 KiB
Python
422 lines
13 KiB
Python
"""
|
|
处理监控 API 路由
|
|
"""
|
|
from flask import Blueprint, jsonify, request
|
|
from services.process_monitor import process_monitor, PROCESS_STEPS
|
|
from models.database import db
|
|
import os
|
|
import logging
|
|
|
|
logger = logging.getLogger('process_monitor_api')
|
|
|
|
# 模板文件路径
|
|
TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config')
|
|
AGENT_TEMPLATE_FILE = os.path.join(TEMPLATE_DIR, 'agent_task_template.txt')
|
|
|
|
bp = Blueprint('process_monitor', __name__, url_prefix='/api/process')
|
|
|
|
|
|
@bp.route('/steps', methods=['GET'])
|
|
def get_step_definitions():
|
|
"""获取处理步骤定义"""
|
|
return jsonify({
|
|
'success': True,
|
|
'steps': PROCESS_STEPS
|
|
})
|
|
|
|
|
|
@bp.route('/start', methods=['POST'])
|
|
def start_process():
|
|
"""
|
|
启动产品处理流程
|
|
|
|
请求体:
|
|
{
|
|
"product_name": "产品名称",
|
|
"category": "分类",
|
|
"subcategory": "子分类"
|
|
}
|
|
"""
|
|
try:
|
|
data = request.get_json()
|
|
|
|
product_name = data.get('product_name')
|
|
if not product_name:
|
|
return jsonify({'success': False, 'error': '缺少产品名称'}), 400
|
|
|
|
category = data.get('category')
|
|
subcategory = data.get('subcategory')
|
|
|
|
# 启动处理流程
|
|
session_id = process_monitor.start_process(product_name, category, subcategory)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'session_id': session_id,
|
|
'message': f'处理流程已启动: {product_name}'
|
|
})
|
|
|
|
except Exception as e:
|
|
logger.error(f"启动处理流程失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@bp.route('/<session_id>/status', methods=['GET'])
|
|
def get_process_status(session_id):
|
|
"""获取处理状态"""
|
|
status = process_monitor.get_session_status(session_id)
|
|
|
|
if not status:
|
|
return jsonify({'success': False, 'error': '会话不存在'}), 404
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'data': status
|
|
})
|
|
|
|
|
|
@bp.route('/<session_id>/pause', methods=['POST'])
|
|
def pause_process(session_id):
|
|
"""暂停处理"""
|
|
if process_monitor.pause_session(session_id):
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '处理已暂停'
|
|
})
|
|
else:
|
|
return jsonify({'success': False, 'error': '无法暂停'}), 400
|
|
|
|
|
|
@bp.route('/<session_id>/resume', methods=['POST'])
|
|
def resume_process(session_id):
|
|
"""继续处理"""
|
|
if process_monitor.resume_session(session_id):
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '处理已继续'
|
|
})
|
|
else:
|
|
return jsonify({'success': False, 'error': '无法继续'}), 400
|
|
|
|
|
|
@bp.route('/<session_id>/stop', methods=['POST'])
|
|
def stop_process(session_id):
|
|
"""停止处理"""
|
|
if process_monitor.stop_session(session_id):
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '处理已停止'
|
|
})
|
|
else:
|
|
return jsonify({'success': False, 'error': '无法停止'}), 400
|
|
|
|
|
|
@bp.route('/active', methods=['GET'])
|
|
def get_active_processes():
|
|
"""获取活动的处理会话"""
|
|
sessions = db.get_active_sessions()
|
|
|
|
# 获取每个会话的步骤信息
|
|
result = []
|
|
for session in sessions:
|
|
steps = db.get_process_steps(session['session_id'])
|
|
result.append({
|
|
'session': session,
|
|
'steps': steps
|
|
})
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'sessions': result,
|
|
'count': len(result)
|
|
})
|
|
|
|
|
|
@bp.route('/recent', methods=['GET'])
|
|
def get_recent_processes():
|
|
"""获取最近的处理会话"""
|
|
limit = request.args.get('limit', 20, type=int)
|
|
sessions = db.get_recent_sessions(limit)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'sessions': sessions,
|
|
'count': len(sessions)
|
|
})
|
|
|
|
|
|
@bp.route('/<session_id>/steps', methods=['GET'])
|
|
def get_process_steps(session_id):
|
|
"""获取处理步骤详情"""
|
|
steps = db.get_process_steps(session_id)
|
|
|
|
# 解析JSON字段
|
|
for step in steps:
|
|
if step.get('step_data'):
|
|
import json
|
|
try:
|
|
step['step_data'] = json.loads(step['step_data'])
|
|
except:
|
|
pass
|
|
if step.get('intervention_data'):
|
|
import json
|
|
try:
|
|
step['intervention_data'] = json.loads(step['intervention_data'])
|
|
except:
|
|
pass
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'steps': steps
|
|
})
|
|
|
|
|
|
@bp.route('/<session_id>/step/<int:step_num>', methods=['GET'])
|
|
def get_step_detail(session_id, step_num):
|
|
"""获取单个步骤详情"""
|
|
step = db.get_step_detail(session_id, step_num)
|
|
|
|
if not step:
|
|
return jsonify({'success': False, 'error': '步骤不存在'}), 404
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'step': step
|
|
})
|
|
|
|
@bp.route('/agent-template', methods=['GET'])
|
|
def get_agent_template():
|
|
"""获取智能体任务文本模板"""
|
|
try:
|
|
if os.path.exists(AGENT_TEMPLATE_FILE):
|
|
with open(AGENT_TEMPLATE_FILE, 'r', encoding='utf-8') as f:
|
|
template = f.read()
|
|
return jsonify({
|
|
'success': True,
|
|
'template': template
|
|
})
|
|
else:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': '模板文件不存在'
|
|
}), 404
|
|
except Exception as e:
|
|
logger.error(f"获取模板失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@bp.route('/agent-template', methods=['POST'])
|
|
def save_agent_template():
|
|
"""保存智能体任务文本模板"""
|
|
try:
|
|
data = request.get_json()
|
|
template = data.get('template', '')
|
|
|
|
if not template:
|
|
return jsonify({'success': False, 'error': '模板内容不能为空'}), 400
|
|
|
|
os.makedirs(TEMPLATE_DIR, exist_ok=True)
|
|
with open(AGENT_TEMPLATE_FILE, 'w', encoding='utf-8') as f:
|
|
f.write(template)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '模板已保存'
|
|
})
|
|
except Exception as e:
|
|
logger.error(f"保存模板失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@bp.route('/agent-template/preview', methods=['POST'])
|
|
def preview_agent_template():
|
|
"""预览填充后的任务文本"""
|
|
try:
|
|
data = request.get_json()
|
|
product_name = data.get('product_name', '示例产品')
|
|
category = data.get('category', '示例类别')
|
|
subcategory = data.get('subcategory', '示例子类别')
|
|
|
|
# 读取模板
|
|
if os.path.exists(AGENT_TEMPLATE_FILE):
|
|
with open(AGENT_TEMPLATE_FILE, 'r', encoding='utf-8') as f:
|
|
template = f.read()
|
|
else:
|
|
return jsonify({'success': False, 'error': '模板文件不存在'}), 404
|
|
|
|
# 填充示例数据
|
|
filled = template.replace('{{product_name}}', product_name)
|
|
filled = filled.replace('{{category}}', category)
|
|
filled = filled.replace('{{subcategory}}', subcategory or '无')
|
|
filled = filled.replace('{{library_results}}', 'ID 101: 示例文章标题A\nID 102: 示例文章标题B\nID 103: 示例文章标题C')
|
|
filled = filled.replace('{{internet_results}}', 'ID 104: 示例互联网抓取标题X\nID 105: 示例互联网抓取标题Y')
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'preview': filled
|
|
})
|
|
except Exception as e:
|
|
logger.error(f"预览模板失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
# ===== 步骤5填充字段模板 API =====
|
|
|
|
FILL_FIELDS_TEMPLATE_FILE = os.path.join(TEMPLATE_DIR, 'agent_fill_fields_template.txt')
|
|
|
|
|
|
@bp.route('/fill-fields-template', methods=['GET'])
|
|
def get_fill_fields_template():
|
|
"""获取步骤5填充字段任务文本模板"""
|
|
try:
|
|
if os.path.exists(FILL_FIELDS_TEMPLATE_FILE):
|
|
with open(FILL_FIELDS_TEMPLATE_FILE, 'r', encoding='utf-8') as f:
|
|
template = f.read()
|
|
return jsonify({
|
|
'success': True,
|
|
'template': template
|
|
})
|
|
else:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': '模板文件不存在'
|
|
}), 404
|
|
except Exception as e:
|
|
logger.error(f"获取填充字段模板失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@bp.route('/fill-fields-template', methods=['POST'])
|
|
def save_fill_fields_template():
|
|
"""保存步骤5填充字段任务文本模板"""
|
|
try:
|
|
data = request.get_json()
|
|
template = data.get('template', '')
|
|
|
|
if not template:
|
|
return jsonify({'success': False, 'error': '模板内容不能为空'}), 400
|
|
|
|
os.makedirs(TEMPLATE_DIR, exist_ok=True)
|
|
with open(FILL_FIELDS_TEMPLATE_FILE, 'w', encoding='utf-8') as f:
|
|
f.write(template)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '模板已保存'
|
|
})
|
|
except Exception as e:
|
|
logger.error(f"保存填充字段模板失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@bp.route('/fill-fields-template/preview', methods=['POST'])
|
|
def preview_fill_fields_template():
|
|
"""预览填充后的步骤5任务文本"""
|
|
try:
|
|
data = request.get_json()
|
|
product_name = data.get('product_name', '示例产品')
|
|
category = data.get('category', '示例类别')
|
|
subcategory = data.get('subcategory', '示例子类别')
|
|
|
|
# 读取模板
|
|
if os.path.exists(FILL_FIELDS_TEMPLATE_FILE):
|
|
with open(FILL_FIELDS_TEMPLATE_FILE, 'r', encoding='utf-8') as f:
|
|
template = f.read()
|
|
else:
|
|
return jsonify({'success': False, 'error': '模板文件不存在'}), 404
|
|
|
|
# 填充示例数据
|
|
filled = template.replace('{{product_name}}', product_name)
|
|
filled = filled.replace('{{category}}', category)
|
|
filled = filled.replace('{{subcategory}}', subcategory or '无')
|
|
filled = filled.replace('{{relevant_content_ids}}', 'ID 101: 示例相关文章A\nID 102: 示例相关文章B\nID 103: 示例相关文章C')
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'preview': filled
|
|
})
|
|
except Exception as e:
|
|
logger.error(f"预览填充字段模板失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
# ===== 步骤6提交审核模板 API =====
|
|
|
|
SUBMIT_TEMPLATE_FILE = os.path.join(TEMPLATE_DIR, 'agent_submit_template.txt')
|
|
|
|
|
|
@bp.route('/submit-template', methods=['GET'])
|
|
def get_submit_template():
|
|
"""获取步骤6提交审核任务文本模板"""
|
|
try:
|
|
if os.path.exists(SUBMIT_TEMPLATE_FILE):
|
|
with open(SUBMIT_TEMPLATE_FILE, 'r', encoding='utf-8') as f:
|
|
template = f.read()
|
|
return jsonify({
|
|
'success': True,
|
|
'template': template
|
|
})
|
|
else:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': '模板文件不存在'
|
|
}), 404
|
|
except Exception as e:
|
|
logger.error(f"获取提交模板失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@bp.route('/submit-template', methods=['POST'])
|
|
def save_submit_template():
|
|
"""保存步骤6提交审核任务文本模板"""
|
|
try:
|
|
data = request.get_json()
|
|
template = data.get('template', '')
|
|
|
|
if not template:
|
|
return jsonify({'success': False, 'error': '模板内容不能为空'}), 400
|
|
|
|
os.makedirs(TEMPLATE_DIR, exist_ok=True)
|
|
with open(SUBMIT_TEMPLATE_FILE, 'w', encoding='utf-8') as f:
|
|
f.write(template)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '模板已保存'
|
|
})
|
|
except Exception as e:
|
|
logger.error(f"保存提交模板失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@bp.route('/submit-template/preview', methods=['POST'])
|
|
def preview_submit_template():
|
|
"""预览填充后的步骤6任务文本"""
|
|
try:
|
|
data = request.get_json()
|
|
product_name = data.get('product_name', '示例产品')
|
|
category = data.get('category', '示例类别')
|
|
subcategory = data.get('subcategory', '示例子类别')
|
|
product_data = data.get('product_data', '{"name": "示例产品", "visible": true}')
|
|
|
|
# 读取模板
|
|
if os.path.exists(SUBMIT_TEMPLATE_FILE):
|
|
with open(SUBMIT_TEMPLATE_FILE, 'r', encoding='utf-8') as f:
|
|
template = f.read()
|
|
else:
|
|
return jsonify({'success': False, 'error': '模板文件不存在'}), 404
|
|
|
|
# 填充示例数据
|
|
filled = template.replace('{{product_name}}', product_name)
|
|
filled = filled.replace('{{category}}', category)
|
|
filled = filled.replace('{{subcategory}}', subcategory or '无')
|
|
filled = filled.replace('{{product_data}}', product_data)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'preview': filled
|
|
})
|
|
except Exception as e:
|
|
logger.error(f"预览提交模板失败: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|