- 模板改为传递内容库数据ID列表而非内容摘要 - 智能体任务改为分析ID对应数据与产品的相关性和参数提取价值 - 智能体输出改为relevant_ids列表+分析说明 - 步骤3保存内容库时记录article_id - 步骤4根据智能体返回的ID从内容库获取实际内容 - 更新_fill_fields适配新的extracted_data格式
261 lines
7.6 KiB
Python
261 lines
7.6 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
|