feat(i18n): replace hardcoded Chinese in backend API responses with t() calls

This commit is contained in:
ghostubborn
2026-04-01 15:32:24 +08:00
parent 3d5e5d024d
commit 74f673a238
5 changed files with 123 additions and 116 deletions

View File

@@ -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(