From 74f673a238d0122f536c9e3951cdde95ed7d4c7f Mon Sep 17 00:00:00 2001 From: ghostubborn Date: Wed, 1 Apr 2026 15:32:24 +0800 Subject: [PATCH] feat(i18n): replace hardcoded Chinese in backend API responses with t() calls --- backend/app/api/graph.py | 51 +++++++------- backend/app/api/report.py | 61 ++++++++--------- backend/app/api/simulation.py | 123 +++++++++++++++++----------------- locales/en.json | 2 + locales/zh.json | 2 + 5 files changed, 123 insertions(+), 116 deletions(-) diff --git a/backend/app/api/graph.py b/backend/app/api/graph.py index 12ff1ba..39d6be5 100644 --- a/backend/app/api/graph.py +++ b/backend/app/api/graph.py @@ -15,6 +15,7 @@ from ..services.graph_builder import GraphBuilderService from ..services.text_processor import TextProcessor from ..utils.file_parser import FileParser from ..utils.logger import get_logger +from ..utils.locale import t from ..models.task import TaskManager, TaskStatus from ..models.project import ProjectManager, ProjectStatus @@ -42,9 +43,9 @@ def get_project(project_id: str): if not project: return jsonify({ "success": False, - "error": f"项目不存在: {project_id}" + "error": t('api.projectNotFound', id=project_id) }), 404 - + return jsonify({ "success": True, "data": project.to_dict() @@ -76,12 +77,12 @@ def delete_project(project_id: str): if not success: return jsonify({ "success": False, - "error": f"项目不存在或删除失败: {project_id}" + "error": t('api.projectDeleteFailed', id=project_id) }), 404 - + return jsonify({ "success": True, - "message": f"项目已删除: {project_id}" + "message": t('api.projectDeleted', id=project_id) }) @@ -95,9 +96,9 @@ def reset_project(project_id: str): if not project: return jsonify({ "success": False, - "error": f"项目不存在: {project_id}" + "error": t('api.projectNotFound', id=project_id) }), 404 - + # 重置到本体已生成状态 if project.ontology: project.status = ProjectStatus.ONTOLOGY_GENERATED @@ -111,7 +112,7 @@ def reset_project(project_id: str): return jsonify({ "success": True, - "message": f"项目已重置: {project_id}", + "message": t('api.projectReset', id=project_id), "data": project.to_dict() }) @@ -160,7 +161,7 @@ def generate_ontology(): if not simulation_requirement: return jsonify({ "success": False, - "error": "请提供模拟需求描述 (simulation_requirement)" + "error": t('api.requireSimulationRequirement') }), 400 # 获取上传的文件 @@ -168,7 +169,7 @@ def generate_ontology(): if not uploaded_files or all(not f.filename for f in uploaded_files): return jsonify({ "success": False, - "error": "请至少上传一个文档文件" + "error": t('api.requireFileUpload') }), 400 # 创建项目 @@ -203,7 +204,7 @@ def generate_ontology(): ProjectManager.delete_project(project.project_id) return jsonify({ "success": False, - "error": "没有成功处理任何文档,请检查文件格式" + "error": t('api.noDocProcessed') }), 400 # 保存提取的文本 @@ -285,12 +286,12 @@ def build_graph(): # 检查配置 errors = [] if not Config.ZEP_API_KEY: - errors.append("ZEP_API_KEY未配置") + errors.append(t('api.zepApiKeyMissing')) if errors: logger.error(f"配置错误: {errors}") return jsonify({ "success": False, - "error": "配置错误: " + "; ".join(errors) + "error": t('api.configError', details="; ".join(errors)) }), 500 # 解析请求 @@ -301,7 +302,7 @@ def build_graph(): if not project_id: return jsonify({ "success": False, - "error": "请提供 project_id" + "error": t('api.requireProjectId') }), 400 # 获取项目 @@ -309,22 +310,22 @@ def build_graph(): if not project: return jsonify({ "success": False, - "error": f"项目不存在: {project_id}" + "error": t('api.projectNotFound', id=project_id) }), 404 - + # 检查项目状态 force = data.get('force', False) # 强制重新构建 if project.status == ProjectStatus.CREATED: return jsonify({ "success": False, - "error": "项目尚未生成本体,请先调用 /ontology/generate" + "error": t('api.ontologyNotGenerated') }), 400 if project.status == ProjectStatus.GRAPH_BUILDING and not force: return jsonify({ "success": False, - "error": "图谱正在构建中,请勿重复提交。如需强制重建,请添加 force: true", + "error": t('api.graphBuilding'), "task_id": project.graph_build_task_id }), 400 @@ -349,7 +350,7 @@ def build_graph(): if not text: return jsonify({ "success": False, - "error": "未找到提取的文本内容" + "error": t('api.textNotFound') }), 400 # 获取本体 @@ -357,7 +358,7 @@ def build_graph(): if not ontology: return jsonify({ "success": False, - "error": "未找到本体定义" + "error": t('api.ontologyNotFound') }), 400 # 创建异步任务 @@ -512,7 +513,7 @@ def build_graph(): "data": { "project_id": project_id, "task_id": task_id, - "message": "图谱构建任务已启动,请通过 /task/{task_id} 查询进度" + "message": t('api.graphBuildStarted', taskId=task_id) } }) @@ -536,7 +537,7 @@ def get_task(task_id: str): if not task: return jsonify({ "success": False, - "error": f"任务不存在: {task_id}" + "error": t('api.taskNotFound', id=task_id) }), 404 return jsonify({ @@ -570,7 +571,7 @@ def get_graph_data(graph_id: str): if not Config.ZEP_API_KEY: return jsonify({ "success": False, - "error": "ZEP_API_KEY未配置" + "error": t('api.zepApiKeyMissing') }), 500 builder = GraphBuilderService(api_key=Config.ZEP_API_KEY) @@ -598,7 +599,7 @@ def delete_graph(graph_id: str): if not Config.ZEP_API_KEY: return jsonify({ "success": False, - "error": "ZEP_API_KEY未配置" + "error": t('api.zepApiKeyMissing') }), 500 builder = GraphBuilderService(api_key=Config.ZEP_API_KEY) @@ -606,7 +607,7 @@ def delete_graph(graph_id: str): return jsonify({ "success": True, - "message": f"图谱已删除: {graph_id}" + "message": t('api.graphDeleted', id=graph_id) }) except Exception as e: diff --git a/backend/app/api/report.py b/backend/app/api/report.py index e05c73c..f803f1f 100644 --- a/backend/app/api/report.py +++ b/backend/app/api/report.py @@ -15,6 +15,7 @@ from ..services.simulation_manager import SimulationManager from ..models.project import ProjectManager from ..models.task import TaskManager, TaskStatus from ..utils.logger import get_logger +from ..utils.locale import t logger = get_logger('mirofish.api.report') @@ -53,9 +54,9 @@ def generate_report(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 - + force_regenerate = data.get('force_regenerate', False) # 获取模拟信息 @@ -65,9 +66,9 @@ def generate_report(): if not state: return jsonify({ "success": False, - "error": f"模拟不存在: {simulation_id}" + "error": t('api.simulationNotFound', id=simulation_id) }), 404 - + # 检查是否已有报告 if not force_regenerate: existing_report = ReportManager.get_report_by_simulation(simulation_id) @@ -78,7 +79,7 @@ def generate_report(): "simulation_id": simulation_id, "report_id": existing_report.report_id, "status": "completed", - "message": "报告已存在", + "message": t('api.reportAlreadyExists'), "already_generated": True } }) @@ -88,21 +89,21 @@ def generate_report(): if not project: return jsonify({ "success": False, - "error": f"项目不存在: {state.project_id}" + "error": t('api.projectNotFound', id=state.project_id) }), 404 graph_id = state.graph_id or project.graph_id if not graph_id: return jsonify({ "success": False, - "error": "缺少图谱ID,请确保已构建图谱" + "error": t('api.missingGraphIdEnsure') }), 400 simulation_requirement = project.simulation_requirement if not simulation_requirement: return jsonify({ "success": False, - "error": "缺少模拟需求描述" + "error": t('api.missingSimRequirement') }), 400 # 提前生成 report_id,以便立即返回给前端 @@ -127,7 +128,7 @@ def generate_report(): task_id, status=TaskStatus.PROCESSING, progress=0, - message="初始化Report Agent..." + message=t('api.initReportAgent') ) # 创建Report Agent @@ -164,7 +165,7 @@ def generate_report(): } ) else: - task_manager.fail_task(task_id, report.error or "报告生成失败") + task_manager.fail_task(task_id, report.error or t('api.reportGenerateFailed')) except Exception as e: logger.error(f"报告生成失败: {str(e)}") @@ -181,7 +182,7 @@ def generate_report(): "report_id": report_id, "task_id": task_id, "status": "generating", - "message": "报告生成任务已启动,请通过 /api/report/generate/status 查询进度", + "message": t('api.reportGenerateStarted'), "already_generated": False } }) @@ -234,7 +235,7 @@ def get_generate_status(): "report_id": existing_report.report_id, "status": "completed", "progress": 100, - "message": "报告已生成", + "message": t('api.reportGenerated'), "already_completed": True } }) @@ -242,7 +243,7 @@ def get_generate_status(): if not task_id: return jsonify({ "success": False, - "error": "请提供 task_id 或 simulation_id" + "error": t('api.requireTaskOrSimId') }), 400 task_manager = TaskManager() @@ -251,7 +252,7 @@ def get_generate_status(): if not task: return jsonify({ "success": False, - "error": f"任务不存在: {task_id}" + "error": t('api.taskNotFound', id=task_id) }), 404 return jsonify({ @@ -294,7 +295,7 @@ def get_report(report_id: str): if not report: return jsonify({ "success": False, - "error": f"报告不存在: {report_id}" + "error": t('api.reportNotFound', id=report_id) }), 404 return jsonify({ @@ -331,7 +332,7 @@ def get_report_by_simulation(simulation_id: str): if not report: return jsonify({ "success": False, - "error": f"该模拟暂无报告: {simulation_id}", + "error": t('api.noReportForSim', id=simulation_id), "has_report": False }), 404 @@ -403,7 +404,7 @@ def download_report(report_id: str): if not report: return jsonify({ "success": False, - "error": f"报告不存在: {report_id}" + "error": t('api.reportNotFound', id=report_id) }), 404 md_path = ReportManager._get_report_markdown_path(report_id) @@ -445,12 +446,12 @@ def delete_report(report_id: str): if not success: return jsonify({ "success": False, - "error": f"报告不存在: {report_id}" + "error": t('api.reportNotFound', id=report_id) }), 404 return jsonify({ "success": True, - "message": f"报告已删除: {report_id}" + "message": t('api.reportDeleted', id=report_id) }) except Exception as e: @@ -501,13 +502,13 @@ def chat_with_report_agent(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 - + if not message: return jsonify({ "success": False, - "error": "请提供 message" + "error": t('api.requireMessage') }), 400 # 获取模拟和项目信息 @@ -517,21 +518,21 @@ def chat_with_report_agent(): if not state: return jsonify({ "success": False, - "error": f"模拟不存在: {simulation_id}" + "error": t('api.simulationNotFound', id=simulation_id) }), 404 - + project = ProjectManager.get_project(state.project_id) if not project: return jsonify({ "success": False, - "error": f"项目不存在: {state.project_id}" + "error": t('api.projectNotFound', id=state.project_id) }), 404 graph_id = state.graph_id or project.graph_id if not graph_id: return jsonify({ "success": False, - "error": "缺少图谱ID" + "error": t('api.missingGraphId') }), 400 simulation_requirement = project.simulation_requirement or "" @@ -585,7 +586,7 @@ def get_report_progress(report_id: str): if not progress: return jsonify({ "success": False, - "error": f"报告不存在或进度信息不可用: {report_id}" + "error": t('api.reportProgressNotAvail', id=report_id) }), 404 return jsonify({ @@ -673,7 +674,7 @@ def get_single_section(report_id: str, section_index: int): if not os.path.exists(section_path): return jsonify({ "success": False, - "error": f"章节不存在: section_{section_index:02d}.md" + "error": t('api.sectionNotFound', index=f"{section_index:02d}") }), 404 with open(section_path, 'r', encoding='utf-8') as f: @@ -949,7 +950,7 @@ def search_graph_tool(): if not graph_id or not query: return jsonify({ "success": False, - "error": "请提供 graph_id 和 query" + "error": t('api.requireGraphIdAndQuery') }), 400 from ..services.zep_tools import ZepToolsService @@ -993,7 +994,7 @@ def get_graph_statistics_tool(): if not graph_id: return jsonify({ "success": False, - "error": "请提供 graph_id" + "error": t('api.requireGraphId') }), 400 from ..services.zep_tools import ZepToolsService diff --git a/backend/app/api/simulation.py b/backend/app/api/simulation.py index 3a0f681..8d44f64 100644 --- a/backend/app/api/simulation.py +++ b/backend/app/api/simulation.py @@ -14,6 +14,7 @@ from ..services.oasis_profile_generator import OasisProfileGenerator from ..services.simulation_manager import SimulationManager, SimulationStatus from ..services.simulation_runner import SimulationRunner, RunnerStatus from ..utils.logger import get_logger +from ..utils.locale import t from ..models.project import ProjectManager logger = get_logger('mirofish.api.simulation') @@ -59,7 +60,7 @@ def get_graph_entities(graph_id: str): if not Config.ZEP_API_KEY: return jsonify({ "success": False, - "error": "ZEP_API_KEY未配置" + "error": t('api.zepApiKeyMissing') }), 500 entity_types_str = request.args.get('entity_types', '') @@ -96,7 +97,7 @@ def get_entity_detail(graph_id: str, entity_uuid: str): if not Config.ZEP_API_KEY: return jsonify({ "success": False, - "error": "ZEP_API_KEY未配置" + "error": t('api.zepApiKeyMissing') }), 500 reader = ZepEntityReader() @@ -105,7 +106,7 @@ def get_entity_detail(graph_id: str, entity_uuid: str): if not entity: return jsonify({ "success": False, - "error": f"实体不存在: {entity_uuid}" + "error": t('api.entityNotFound', id=entity_uuid) }), 404 return jsonify({ @@ -129,7 +130,7 @@ def get_entities_by_type(graph_id: str, entity_type: str): if not Config.ZEP_API_KEY: return jsonify({ "success": False, - "error": "ZEP_API_KEY未配置" + "error": t('api.zepApiKeyMissing') }), 500 enrich = request.args.get('enrich', 'true').lower() == 'true' @@ -197,21 +198,21 @@ def create_simulation(): if not project_id: return jsonify({ "success": False, - "error": "请提供 project_id" + "error": t('api.requireProjectId') }), 400 project = ProjectManager.get_project(project_id) if not project: return jsonify({ "success": False, - "error": f"项目不存在: {project_id}" + "error": t('api.projectNotFound', id=project_id) }), 404 graph_id = data.get('graph_id') or project.graph_id if not graph_id: return jsonify({ "success": False, - "error": "项目尚未构建图谱,请先调用 /api/graph/build" + "error": t('api.graphNotBuilt') }), 400 manager = SimulationManager() @@ -408,7 +409,7 @@ def prepare_simulation(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 manager = SimulationManager() @@ -417,7 +418,7 @@ def prepare_simulation(): if not state: return jsonify({ "success": False, - "error": f"模拟不存在: {simulation_id}" + "error": t('api.simulationNotFound', id=simulation_id) }), 404 # 检查是否强制重新生成 @@ -436,7 +437,7 @@ def prepare_simulation(): "data": { "simulation_id": simulation_id, "status": "ready", - "message": "已有完成的准备工作,无需重复生成", + "message": t('api.alreadyPrepared'), "already_prepared": True, "prepare_info": prepare_info } @@ -449,7 +450,7 @@ def prepare_simulation(): if not project: return jsonify({ "success": False, - "error": f"项目不存在: {state.project_id}" + "error": t('api.projectNotFound', id=state.project_id) }), 404 # 获取模拟需求 @@ -457,7 +458,7 @@ def prepare_simulation(): if not simulation_requirement: return jsonify({ "success": False, - "error": "项目缺少模拟需求描述 (simulation_requirement)" + "error": t('api.projectMissingRequirement') }), 400 # 获取文档文本 @@ -612,7 +613,7 @@ def prepare_simulation(): "simulation_id": simulation_id, "task_id": task_id, "status": "preparing", - "message": "准备任务已启动,请通过 /api/simulation/prepare/status 查询进度", + "message": t('api.prepareStarted'), "already_prepared": False, "expected_entities_count": state.entities_count, # 预期的Agent总数 "entity_types": state.entity_types # 实体类型列表 @@ -680,7 +681,7 @@ def get_prepare_status(): "simulation_id": simulation_id, "status": "ready", "progress": 100, - "message": "已有完成的准备工作", + "message": t('api.alreadyPrepared'), "already_prepared": True, "prepare_info": prepare_info } @@ -696,13 +697,13 @@ def get_prepare_status(): "simulation_id": simulation_id, "status": "not_started", "progress": 0, - "message": "尚未开始准备,请调用 /api/simulation/prepare 开始", + "message": t('api.notStartedPrepare'), "already_prepared": False } }) return jsonify({ "success": False, - "error": "请提供 task_id 或 simulation_id" + "error": t('api.requireTaskOrSimId') }), 400 task_manager = TaskManager() @@ -720,7 +721,7 @@ def get_prepare_status(): "task_id": task_id, "status": "ready", "progress": 100, - "message": "任务已完成(准备工作已存在)", + "message": t('api.taskCompletedPrepared'), "already_prepared": True, "prepare_info": prepare_info } @@ -728,7 +729,7 @@ def get_prepare_status(): return jsonify({ "success": False, - "error": f"任务不存在: {task_id}" + "error": t('api.taskNotFound', id=task_id) }), 404 task_dict = task.to_dict() @@ -757,7 +758,7 @@ def get_simulation(simulation_id: str): if not state: return jsonify({ "success": False, - "error": f"模拟不存在: {simulation_id}" + "error": t('api.simulationNotFound', id=simulation_id) }), 404 result = state.to_dict() @@ -1061,7 +1062,7 @@ def get_simulation_profiles_realtime(simulation_id: str): if not os.path.exists(sim_dir): return jsonify({ "success": False, - "error": f"模拟不存在: {simulation_id}" + "error": t('api.simulationNotFound', id=simulation_id) }), 404 # 确定文件路径 @@ -1164,7 +1165,7 @@ def get_simulation_config_realtime(simulation_id: str): if not os.path.exists(sim_dir): return jsonify({ "success": False, - "error": f"模拟不存在: {simulation_id}" + "error": t('api.simulationNotFound', id=simulation_id) }), 404 # 配置文件路径 @@ -1269,7 +1270,7 @@ def get_simulation_config(simulation_id: str): if not config: return jsonify({ "success": False, - "error": f"模拟配置不存在,请先调用 /prepare 接口" + "error": t('api.configNotFound') }), 404 return jsonify({ @@ -1297,7 +1298,7 @@ def download_simulation_config(simulation_id: str): if not os.path.exists(config_path): return jsonify({ "success": False, - "error": "配置文件不存在,请先调用 /prepare 接口" + "error": t('api.configFileNotFound') }), 404 return send_file( @@ -1341,7 +1342,7 @@ def download_simulation_script(script_name: str): if script_name not in allowed_scripts: return jsonify({ "success": False, - "error": f"未知脚本: {script_name},可选: {allowed_scripts}" + "error": t('api.unknownScript', name=script_name, allowed=allowed_scripts) }), 400 script_path = os.path.join(scripts_dir, script_name) @@ -1349,7 +1350,7 @@ def download_simulation_script(script_name: str): if not os.path.exists(script_path): return jsonify({ "success": False, - "error": f"脚本文件不存在: {script_name}" + "error": t('api.scriptFileNotFound', name=script_name) }), 404 return send_file( @@ -1389,7 +1390,7 @@ def generate_profiles(): if not graph_id: return jsonify({ "success": False, - "error": "请提供 graph_id" + "error": t('api.requireGraphId') }), 400 entity_types = data.get('entity_types') @@ -1406,7 +1407,7 @@ def generate_profiles(): if filtered.filtered_count == 0: return jsonify({ "success": False, - "error": "没有找到符合条件的实体" + "error": t('api.noMatchingEntities') }), 400 generator = OasisProfileGenerator() @@ -1491,7 +1492,7 @@ def start_simulation(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 platform = data.get('platform', 'parallel') @@ -1506,18 +1507,18 @@ def start_simulation(): if max_rounds <= 0: return jsonify({ "success": False, - "error": "max_rounds 必须是正整数" + "error": t('api.maxRoundsPositive') }), 400 except (ValueError, TypeError): return jsonify({ "success": False, - "error": "max_rounds 必须是有效的整数" + "error": t('api.maxRoundsInvalid') }), 400 if platform not in ['twitter', 'reddit', 'parallel']: return jsonify({ "success": False, - "error": f"无效的平台类型: {platform},可选: twitter/reddit/parallel" + "error": t('api.invalidPlatform', platform=platform) }), 400 # 检查模拟是否已准备好 @@ -1527,7 +1528,7 @@ def start_simulation(): if not state: return jsonify({ "success": False, - "error": f"模拟不存在: {simulation_id}" + "error": t('api.simulationNotFound', id=simulation_id) }), 404 force_restarted = False @@ -1554,7 +1555,7 @@ def start_simulation(): else: return jsonify({ "success": False, - "error": f"模拟正在运行中,请先调用 /stop 接口停止,或使用 force=true 强制重新开始" + "error": t('api.simRunningForceHint') }), 400 # 如果是强制模式,清理运行日志 @@ -1573,7 +1574,7 @@ def start_simulation(): # 准备工作未完成 return jsonify({ "success": False, - "error": f"模拟未准备好,当前状态: {state.status.value},请先调用 /prepare 接口" + "error": t('api.simNotReady', status=state.status.value) }), 400 # 获取图谱ID(用于图谱记忆更新) @@ -1590,7 +1591,7 @@ def start_simulation(): if not graph_id: return jsonify({ "success": False, - "error": "启用图谱记忆更新需要有效的 graph_id,请确保项目已构建图谱" + "error": t('api.graphIdRequiredForMemory') }), 400 logger.info(f"启用图谱记忆更新: simulation_id={simulation_id}, graph_id={graph_id}") @@ -1663,7 +1664,7 @@ def stop_simulation(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 run_state = SimulationRunner.stop_simulation(simulation_id) @@ -2011,7 +2012,7 @@ def get_simulation_posts(simulation_id: str): "platform": platform, "count": 0, "posts": [], - "message": "数据库不存在,模拟可能尚未运行" + "message": t('api.dbNotExist') } }) @@ -2197,33 +2198,33 @@ def interview_agent(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 if agent_id is None: return jsonify({ "success": False, - "error": "请提供 agent_id" + "error": t('api.requireAgentId') }), 400 if not prompt: return jsonify({ "success": False, - "error": "请提供 prompt(采访问题)" + "error": t('api.requirePrompt') }), 400 # 验证platform参数 if platform and platform not in ("twitter", "reddit"): return jsonify({ "success": False, - "error": "platform 参数只能是 'twitter' 或 'reddit'" + "error": t('api.invalidInterviewPlatform') }), 400 # 检查环境状态 if not SimulationRunner.check_env_alive(simulation_id): return jsonify({ "success": False, - "error": "模拟环境未运行或已关闭。请确保模拟已完成并进入等待命令模式。" + "error": t('api.envNotRunning') }), 400 # 优化prompt,添加前缀避免Agent调用工具 @@ -2251,7 +2252,7 @@ def interview_agent(): except TimeoutError as e: return jsonify({ "success": False, - "error": f"等待Interview响应超时: {str(e)}" + "error": t('api.interviewTimeout', error=str(e)) }), 504 except Exception as e: @@ -2318,20 +2319,20 @@ def interview_agents_batch(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 if not interviews or not isinstance(interviews, list): return jsonify({ "success": False, - "error": "请提供 interviews(采访列表)" + "error": t('api.requireInterviews') }), 400 # 验证platform参数 if platform and platform not in ("twitter", "reddit"): return jsonify({ "success": False, - "error": "platform 参数只能是 'twitter' 或 'reddit'" + "error": t('api.invalidInterviewPlatform') }), 400 # 验证每个采访项 @@ -2339,26 +2340,26 @@ def interview_agents_batch(): if 'agent_id' not in interview: return jsonify({ "success": False, - "error": f"采访列表第{i+1}项缺少 agent_id" + "error": t('api.interviewListMissingAgentId', index=i+1) }), 400 if 'prompt' not in interview: return jsonify({ "success": False, - "error": f"采访列表第{i+1}项缺少 prompt" + "error": t('api.interviewListMissingPrompt', index=i+1) }), 400 # 验证每项的platform(如果有) item_platform = interview.get('platform') if item_platform and item_platform not in ("twitter", "reddit"): return jsonify({ "success": False, - "error": f"采访列表第{i+1}项的platform只能是 'twitter' 或 'reddit'" + "error": t('api.interviewListInvalidPlatform', index=i+1) }), 400 # 检查环境状态 if not SimulationRunner.check_env_alive(simulation_id): return jsonify({ "success": False, - "error": "模拟环境未运行或已关闭。请确保模拟已完成并进入等待命令模式。" + "error": t('api.envNotRunning') }), 400 # 优化每个采访项的prompt,添加前缀避免Agent调用工具 @@ -2389,7 +2390,7 @@ def interview_agents_batch(): except TimeoutError as e: return jsonify({ "success": False, - "error": f"等待批量Interview响应超时: {str(e)}" + "error": t('api.batchInterviewTimeout', error=str(e)) }), 504 except Exception as e: @@ -2445,27 +2446,27 @@ def interview_all_agents(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 if not prompt: return jsonify({ "success": False, - "error": "请提供 prompt(采访问题)" + "error": t('api.requirePrompt') }), 400 # 验证platform参数 if platform and platform not in ("twitter", "reddit"): return jsonify({ "success": False, - "error": "platform 参数只能是 'twitter' 或 'reddit'" + "error": t('api.invalidInterviewPlatform') }), 400 # 检查环境状态 if not SimulationRunner.check_env_alive(simulation_id): return jsonify({ "success": False, - "error": "模拟环境未运行或已关闭。请确保模拟已完成并进入等待命令模式。" + "error": t('api.envNotRunning') }), 400 # 优化prompt,添加前缀避免Agent调用工具 @@ -2492,7 +2493,7 @@ def interview_all_agents(): except TimeoutError as e: return jsonify({ "success": False, - "error": f"等待全局Interview响应超时: {str(e)}" + "error": t('api.globalInterviewTimeout', error=str(e)) }), 504 except Exception as e: @@ -2549,7 +2550,7 @@ def get_interview_history(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 history = SimulationRunner.get_interview_history( @@ -2608,7 +2609,7 @@ def get_env_status(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 env_alive = SimulationRunner.check_env_alive(simulation_id) @@ -2617,9 +2618,9 @@ def get_env_status(): env_status = SimulationRunner.get_env_status_detail(simulation_id) if env_alive: - message = "环境正在运行,可以接收Interview命令" + message = t('api.envRunning') else: - message = "环境未运行或已关闭" + message = t('api.envNotRunningShort') return jsonify({ "success": True, @@ -2676,7 +2677,7 @@ def close_simulation_env(): if not simulation_id: return jsonify({ "success": False, - "error": "请提供 simulation_id" + "error": t('api.requireSimulationId') }), 400 result = SimulationRunner.close_simulation_env( diff --git a/locales/en.json b/locales/en.json index 0a506e2..a802e94 100644 --- a/locales/en.json +++ b/locales/en.json @@ -333,6 +333,8 @@ "batchInterviewTimeout": "Batch interview response timed out: {error}", "globalInterviewTimeout": "Global interview response timed out: {error}", "envRunning": "Environment is running and ready for Interview commands", + "envNotRunningShort": "Environment not running or closed", + "requireGraphIdAndQuery": "Please provide graph_id and query", "initReportAgent": "Initializing Report Agent..." } } diff --git a/locales/zh.json b/locales/zh.json index aa48799..c95f9c3 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -333,6 +333,8 @@ "batchInterviewTimeout": "等待批量Interview响应超时: {error}", "globalInterviewTimeout": "等待全局Interview响应超时: {error}", "envRunning": "环境正在运行,可以接收Interview命令", + "envNotRunningShort": "环境未运行或已关闭", + "requireGraphIdAndQuery": "请提供 graph_id 和 query", "initReportAgent": "初始化Report Agent..." } }