(session_id, script_index, retry_count)
| 181 | 'retryCount': retry_count, |
| 182 | 'scriptIndex': script_index, |
| 183 | 'session_id': session_id |
| 184 | }) |
| 185 | |
| 186 | @app.route('/stream_log') |
| 187 | def stream_log(): |
| 188 | def generate(): |
| 189 | log_file = 'log.txt' |
| 190 | if not os.path.exists(log_file): |
| 191 | open(log_file, 'w', encoding='utf-8').close() |
| 192 | |
| 193 | with open(log_file, 'r', encoding='utf-8', errors='replace') as f: |
| 194 | lines = f.readlines() |
| 195 | for line in lines[-5:]: |
| 196 | yield f"data: {json.dumps({'text': line.strip()})}\n\n" |
| 197 | |
| 198 | while True: |
| 199 | line = f.readline() |
| 200 | if line: |
| 201 | yield f"data: {json.dumps({'text': line.strip()})}\n\n" |
| 202 | else: |
| 203 | time.sleep(0.5) |
| 204 | |
| 205 | return Response(generate(), mimetype='text/event-stream') |
| 206 | |
| 207 | @app.route('/get_llm_config') |
| 208 | def get_llm_config(): |
| 209 | try: |
| 210 | data = load_config_file() |
| 211 | active_id = data.get('active_id') |
| 212 | # 返回当前激活配置的完整信息,方便前端直接使用 |
| 213 | active_config = None |
| 214 | for c in data.get('configs', []): |
| 215 | if c.get('id') == active_id: |
| 216 | active_config = c |
| 217 | break |
| 218 | return jsonify({ |
| 219 | 'status': 'success', |
| 220 | 'configs': data.get('configs', []), |
| 221 | 'active_id': active_id, |
| 222 | 'active_config': active_config |
| 223 | }) |
| 224 | except Exception as e: |
| 225 | logger.error(f"获取 LLM 配置失败:{str(e)}") |
| 226 | return jsonify({'status': 'error', 'message': f'获取配置失败:{str(e)}'}), 500 |
| 227 | |
| 228 | @app.route('/save_llm_config', methods=['POST']) |
| 229 | def save_llm_config(): |
| 230 | try: |
| 231 | config_entry = request.get_json() |
| 232 | |
| 233 | required_fields = ['api_key', 'base_url', 'model'] |
| 234 | for field in required_fields: |
| 235 | if field not in config_entry: |
| 236 | return jsonify({'status': 'error', 'message': f'缺少必需字段:{field}'}), 400 |
| 237 | |
| 238 | data = load_config_file() |
| 239 | configs = data.get('configs', []) |
| 240 |
nothing calls this directly
no test coverage detected